Google Answers Logo
View Question
 
Q: Compare time needed to pass array ( No Answer,   6 Comments )
Question  
Subject: Compare time needed to pass array
Category: Computers > Programming
Asked by: jcoen-ga
List Price: $10.50
Posted: 31 Jul 2005 11:08 PDT
Expires: 30 Aug 2005 11:08 PDT
Question ID: 550086
I need some kind of timer object to compare the ammount of time
required to pass a large array by value, vs. by reference. In the Main
method, two methods, one expecting it to be passed by value, the other
by reference, should be called with the timer object running on each
one, as when everything is done executing, it should output the
ammount of time it took to pass the large array by value and the
ammount of time it took to pass it by reference.

Clarification of Question by jcoen-ga on 31 Jul 2005 16:56 PDT
Let's try in Java... I found this website:
http://www.javapractices.com/Topic3.cjp  That tutorial is a little to
advanced for this though, I just want to time passing as a value and
passing as a reference for a very big array and see which is faster...

Request for Question Clarification by mathtalk-ga on 02 Aug 2005 05:47 PDT
Hi, jcoen-ga:

As Commenters have pointed out, the alternatives of passing an array
by value or by reference make the most sense in C++, or if you are
willing to accept passing of a pointer as the equivalent to passing by
reference (which it certainly is), in C.

Passing an array by value in C or C++ means allocating the array on
the stack (and copying or "pushing" the entry values there).  If the
array is large enough, one can run out of stack space (not as much of
a problem in 32- and 64-bit environments).  [In Algol, or at least in
the Burroughs/Unisys implementation of it, everything is on the stack
(or on a stack).]

There is a part of your Question which has to do with how to perform
timing tests, and of course this sort of goal makes sense in
essentially every programming environment.  Would you be interested in
an Answer that focuses on a simple test of repeatedly calling a
function in C that passes an array by value or "by reference" (ie.
passing a pointer instead of the array values)?

regards, mathtalk-ga
Answer  
There is no answer at this time.

Comments  
Subject: Re: Compare time needed to pass array
From: efn-ga on 31 Jul 2005 16:16 PDT
 
I suggest you specify the operating system under which the test
program is to run and the programming language with which you wish to
test.
Subject: Re: Compare time needed to pass array
From: efn-ga on 31 Jul 2005 23:40 PDT
 
Java doesn't give you two different ways to pass an array to a
function.  All you can do is pass a reference to an array.  That means
that the array is not copied and the called function can modify it.  I
suppose if you wanted to simulate passing an array by value, you could
make a copy of the array and pass a reference to the copy, but if you
did that, the difference would only be in the calling function.  The
interface to the called function would be the same in both cases.

You might be interested in these discussions:

http://www.yoda.arachsys.com/java/passing.html

http://www.jguru.com/faq/view.jsp?EID=430996
Subject: Re: Compare time needed to pass array
From: rhansenne-ga on 01 Aug 2005 03:38 PDT
 
A quick and dirty way to quickly time some code in Java:


long startTime = System.currentTimeMillis();

// execute code here...

long endTime = System.currentTimeMillis();
System.out.println("Duration: "+(endTime - startTime)+"ms");
Subject: Re: Compare time needed to pass array
From: sucker5-ga on 01 Aug 2005 09:07 PDT
 
efn was correct, in java and many other true oo lang's you always pass
by "reference". But if you are using something like c++ you can do
either. If you pass a pointer or by reference you are just passing the
address of the array. If you pass the entire array, a copy is made so
you have O(n) time assuming it is an actual array and not another data
structure. It depends on ,n, the number of elements in the array.
Subject: Re: Compare time needed to pass array
From: edheal-ga on 02 Aug 2005 03:38 PDT
 
To get an more accurate measurment for timing you need to repeat the
passing by value/reference many times and take the average. This
removed inconsistencies due to the operating system performing other
tasks. In addition ensure that you have no other programs running.
Subject: Re: Compare time needed to pass array
From: flaviusmaximus-ga on 10 Aug 2005 11:57 PDT
 
sorry to butt in.  I can't provide code at this time.  Additionally,
as was pointed out your specific environment may affect results.  That
said, my experience has been pretty consistent.  I have real life
example using ORACLE and PL/SQL arrays and Cobol program.

At the time, PL/SQL ARRAYS were pass by value only.  When converted to
Cobol using pass by reference, we learned that 90% of the cost of the
process was overhead of copying arrays.  This is an actual real worl
app stat, not an estimate.

FYI, today pl/sql support and "NOCOPY" parameter which passes by reference.

So, this may not be your environment, and your milage may vary but in
general if the arrays are big enough, then an order of magnitude is
not out of the questions.

Good luck. Kevin

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy