Return Objects by Value/Reference?
Could someone please tell me which of this methods would be faster:
import java.util.ArrayList;
import java.util.List;
publicclass Test{
publicstaticfinalint MAX_ITER = 2000000;
publicstaticvoid main(String[] args){
long begin, end = 0;
//Return Object by Value
begin = System.currentTimeMillis();
List list1 = returnByValue();
end = System.currentTimeMillis();
System.out.println(end - begin);
//Return Object by reference
begin = System.currentTimeMillis();
List list2 =new ArrayList();
returnByRef(list2);
end = System.currentTimeMillis();
System.out.println(end - begin);
}
publicstatic List returnByValue(){
List list =new ArrayList();
for(int i=0; i<MAX_ITER; i++)
list.add(new Integer(i));
return list;
}
publicstaticvoid returnByRef(List list){
for(int i=0; i><MAX_ITER; i++)
list.add(new Integer(i));
}
}
From what I understand, returnByValue would be slower as it involves creating a local List, and then returning it by way of copying itself? But surprisingly, both methods take almost identical times to run. Also, please run each method individually one at a time per run, otherwise for some reason, the second method always takes longer if ran simultaneously. Thanks.>
[2459 byte] By [
zOOstara] at [2007-11-26 13:41:15]

Utter nonsense.
First of all, everything is passed by value in Java.
If you're really concerned, write a test and time both. I think this is an example of a micro-optimization that isn't worth the thought.
The thing that DOES matter is design. I think returning that object instead of void is a better design. Values that are passed in are best left unaltered, IMO. It's an example of a "side effect free function". If I pass you an array, I shouldn't have to worry about it being changed on me.
%
Your own test tells you that it doesn't matter. Why don't you believe it?%
Returning the local List does *not* copy the List. You just get a reference to the single List that was created within returnByValue.
> Returning the local List does *not* copy the List.
> You just get a reference to the single List that was
> created within returnByValue.
Correct.
If you were writing C++ it would call a copy constructor to duplicate the value to be sent back, but that's not the way Java's memory model works. As doremi correctly points out, the reference to the local object is returned.
Sounds like someone needs a remedial lesson in Java's memory model.
%
> Returning the local List does *not* copy the List.> You just get a reference to the single List that was> created within returnByValue.Thanks for the "valuable" contribution to my post, your explanation indeed does provide an insight.
> > Returning the local List does *not* copy the List.
> > You just get a reference to the single List that was
> > created within returnByValue.
>
> Thanks for the "valuable" contribution to my post,
> your explanation indeed does provide an insight.
You're welcome. I assume "valuable" is in quotes because it is a pun on "values"?
> You're welcome. I assume "valuable" is in quotes> because it is a pun on "values"?its more of a reminder to some who contribute anything but value to posts, maybe just to increase their post count.
> its more of a reminder to some who contribute> anything but value to posts, maybe just to increase> their post count.If you're refering to me, I'm hardly here to increase my post count.%
> > You're welcome. I assume "valuable" is in quotes
> > because it is a pun on "values"?
>
> its more of a reminder to some who contribute
> anything but value to posts, maybe just to increase
> their post count.
Okay, I thought about that, too, because I thought that your post did sound a bit sarcastic. However, I decided to try to assume the best out of your post, because my post was most certainly of value. It told you exactly what you needed to know: that no copy was being made. As you can see by Reply #4, duffymo agreed that my post was correct and that the information was useful.
I don't care about my post count. Maybe next time I'll help people who are more appreciative of my information, instead of helping you.
zOOstar is just another victim of C++, as far as I can see. Symptoms of the condition include the desire to run microbenchmarks and the belief in complicated scenarios that do not exist in Java.
doremifasollatido I was not refering to you in my reply.
> Okay, I thought about that, too, because I thought
> that your post did sound a bit sarcastic.
Your post was most useful. I think s/he's refering to me.
> I don't care about my post count. Maybe next time
> I'll help people who are more appreciative of my
> information, instead of helping you.
Actually, I think s/he's most appreciative of your excellent contribution. I believe I'm the problem.
I think that telling people to believe the evidence in front of them is a good service to provide. You ran a test that told you the two were virtually identical. I gave you another viewpoint that said side effect free functions should be preferred, especially in situations like this one. No post count boosting there. That's good information. You just need to be sharp enough to realize it.
%
> zOOstar is just another victim of C++, as far as I
> can see. Symptoms of the condition include the desire
> to run microbenchmarks and the belief in complicated
> scenarios that do not exist in Java.
I'm betting zOOstar has never written C++. Just misunderstood how Java works, that's all.
%
> doremifasollatido > I was not refering to you in my reply.You quoted me, not duffymo, so I assumed your reply referred to me. And, his information was just as useful as mine.
> You quoted me, not duffymo, so I assumed your reply
> referred to me. And, his information was just as
> useful as mine.
Yours was the coup de grace, doremi. Even I liked it better.
I didn't see it because I rarely read the code that's presented in these examples closely enough. It's not always worth the time.
%
> > You quoted me, not duffymo, so I assumed your reply
> > referred to me. And, his information was just as
> > useful as mine.
>
> Yours was the coup de grace, doremi. Even I liked it
> better.
Thanks. :)
> I didn't see it because I rarely read the code that's
> presented in these examples closely enough. It's not
> always worth the time.
I didn't see your posts until after I had finished typing my first post. I was always confused in C++ (never studied it, then had to use it for work) as to what was returned--a copy or the local value (I was trying to figure out when the copy constructor and the destructor were called, I think, because data was sometimes corrupted [I don't know--it has been a while]). When I started Java and learned how Java works, I thought everything was much easier to deal with in Java.
>I was always confused in C++
> (never studied it, then had to use it for work) as to
> what was returned--a copy or the local value (I was
> trying to figure out when the copy constructor and
> the destructor were called, I think, because data was
> sometimes corrupted [I don't know--it has been a
> while]). When I started Java and learned how Java
> works, I thought everything was much easier to deal
> with in Java.
I studied C++ hard, used it at work, and even liked it, but I think Java is easier.
%
> I studied C++ hard, used it at work, and even liked
> it, but I think Java is easier.
C++ is a beautiful and powerful language that hides a lot of pitfalls. C++
doesn't just shoot you in your foot when you mistakenly make a small
error; it mercilessly stabs you in the back and ruthlessly cuts all your
limbs off when you don't pay attention for a minute. And then it bites your
head off. I like the way C++ handles certain things behind your back.
I dislike it when there's no adequate documentation w.r.t. copy ctors,
non virtual dtors, crippled value semantics overloaded operators etc. etc.
C++ is a fighting language while the Java language is just a comfy chair ;-)
kind regards,
Jos (< been there, done that)
JosAHa at 2007-7-21 15:53:50 >
