Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the dfference between these two loops?

 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop 1

Loop 2


Why the Loop1 is preferred over the second one? Is it because of the new reference created everytime? Does it has some performance issues?

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me, the difference between those two loops is the scope of the object. I prefer the second loop because it keeps the scope of the object as small as possible, which I like.

As for performance, I don't know, I usually don't work on systems where this level of performance optimization would be meaningful (not in years anyway). I would think that with compilers being as smart as they are now, the performance would be about the same as they are allowed to re-arrange declarations and assignments to optimize speed. But perhaps someone else has better experience than I do.
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the second loop the newly created objects inside the loop will take more time to get Garbage collected as the number of references are more when compared to the first loop.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balagopal Kannampallil wrote:In the second loop the newly created objects inside the loop will take more time to get Garbage collected as the number of references are more when compared to the first loop.


Well we are adding those Objects in arrayList so I dont think that GC will come into picture because in both of these loops arrayList visibility is same.

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Himanshu,

Here is the quote from EFH that I have copied from my old post

EFH wrote:a reference is just a reserved spot on the stack, and absolutely nothing is done at runtime to "create" one. Therefore this loop has an empty body; the Hotspot compiler is likely to optimize it away completely.


A reference has any importance when an object is associated to it. So in your case scope is the only thing that differs in given two loops. And it's already been pointed out by other people.
This should surely clear the confusion.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i dont think there is not going to be any difference in the performance with the 2 loops. I consider it to be for more about readability that people prefer the first method.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your replies. In my code review i coded using the Loop2 style.... and it came back as a defect..... so i thought that there may b some difference which I dont know..... so I posted it here....

Thanks again everyone for your replies.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Gupta wrote:Thanks everyone for your replies. In my code review i coded using the Loop2 style.... and it came back as a defect....


It might be considered a defect in C++, but in Java, it honestly makes no difference at all. The compiler will generate the exact same bytecode, in fact, with one small difference: in version 1, obj, arr, and i will be in registers 1, 2, and 3, respectively, while in version 2 they'll be in 3, 1, and 2 -- just because the order in which they're first mentioned changes.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be considered a defect in C++, but in Java, ....... order in which they're first mentioned changes.


Thanks for your reply Ernest.
I got something which states that Loop1 style is more efficient as compared to Loop2. But the reason is not given there and I am also not able to find out. src

 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My 2 cents is that I prefer to declare the reference inside the loop, then its clear that its only used inside the loop. If its declared outside, in 6 months time you might wonder is that supposed to be used somewhere outside the loop.

I think in any meaningful application, these micro-optimizations are far less important than the efficiency of database access, remote method calls, webservice calls etc
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's work the same as:

 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marky Vasconcellos wrote:It's work the same as:


You are right Marky but the question is what makes Loop1 more efficient?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Gupta wrote:You are right Marky but the question is what makes Loop1 more efficient?

Nothing does - it's not more efficient. The author of JAC_052 is, how can I put this gently? Full of crap. Ignore it.
 
Marky Vasconcellos
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry.. i post that to say.. the three are the same.. no one object will be have reference to null cause they are all in the ArrayList.. the only difference is the size of the final compiled class.. but some bytes less or higher, and no significant.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic