• Post Reply Bookmark Topic Watch Topic
  • New Topic

instance in a for loop - right /wrong/doesn't matter ?  RSS feed

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiousity:

What is the diff (if any) between this:

for (int x=0; x<1000; x++)
{
boolean flag=obj[x].getInfo();
}

and this:

boolean flag;
for (int x=0; x<1000; x++)
{
flag=obj[x].getInfo();
}

Thanks for any help


 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding performance, none. It should compile to exactly the same byte code, as far as I know.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about memory usage?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are stored in machine registers. As Ilja says, these will compile to identical code.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if the local variable is on the stack instead of in a register (I'm not familiar with the JVM enough, but I assume it works the same as C/C++), variables in a loop are reused so there's really only one variable called flag.

The only difference here is that flag isn't available outside of the first loop. That being said, I strive to make variables have the smallest scope possible as it avoids name collisions and makes it clear that the variable isn't used outside the loop without having to read the rest of the method.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so basically it is better to user the first option:
for (int x=0; x<1000; x++)
{
boolean flag=obj[x].getInfo();
}

because:
a. once it's out the loop the stack is free
b. collisions (can't use it outside the loop)

agree?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. Generally, your first order priority should be to express the intentions of the code as well as possible. Let the compiler care about optimizing execution - the modern ones are quite good at it. And once you really encounter a performance problem, you can work on it, with the knowledge that you are working on a real, concrete problem, and that your code is easy to understand and change - and therefore also easy to performance optimize.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Primrose:
a. once it's out the loop the stack is free
Agreed on all but this. Again, extrapolating from C/C++, the stack is only unrolled when the method returns -- not partially during the method execution. It's certainly possible to partially unroll the stack after the loop, but I doubt the JVM does this (and you wouldn't count on it anyway since it's not part of the specification) since it adds complexity for little gain.

I do believe, however, that if you had another loop with a boolean variable inside it, the JVM would reuse the same space in the stack for both variables since they aren't used at the same time. Again, this is another optimization left up to the compiler or JVM.

Ilja's advice takes precedence above all that: write clear code that expresses your intentions first.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David, you are right. Of course, depending on the architecture the program runs on, it might not make use of the stack at all - it might just use a register in this case, for example.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!