Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable creation inside of a loop  RSS feed

 
Thurston Moore
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I saving any memory by moving variable creation (someObj in the example below) outside of a loop as follows?

Object someObj;
Collection objects;

while (something)
{
someObj = new Object(x, y);
objs.add(someObj);
}

versus:

Collection objects;

while (something)
{
Object someObj = new Object(x, y);
objs.add(someObj);
}

Thank you!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't move variable creation but variable declaration outside the loop.

Empirically I get a performance-loss by your technique from about 1% in a loop just creating 10 million objects in less than two seconds.

If you create less elements, or do some real work in your loop, the difference will get smaller of course.
Obfuscating the own code by such technique isn't wise, especially if there is not a prooven bottleneck.

Premature optimization is the root of all ...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan's 1% performance change is likely an illusion. Try it yourself: a standard Java compiler will emit identical bytecode for the two versions of the program.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will save more memory and resources by initializing the resource to null after you are done with it. This will save memory so that you don't need to wait until GC takes place.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ernest:
Exactly your argument I heared in my ear when reading the question, and my comment was already written, claiming 'same bytecode', but don't being sure about that, I wrote the two classes, and compared them: both 369 bytes, before hitting 'submit'.
Well - that's no proof, I thought, and called

I'm not able to interpret this, but as far as I can tell, this is not
identical bytecode.
But without using a realtime OS, my measurements might be garbage though.

What is your source of information?

@Kishore:
Pulling the variable out of the loop is useless.
Nulling it later is additional noise and makes rarely sense in normal places - here I only see disadvantages.

Keep the scope of variable clean, to make concepts visible.
Saving a microsecond after years of uptime will not pay the minutes, generations of programmers need to find out, that 'Collection objects', declared before the loop is only used inside the loop.
[ May 13, 2005: Message edited by: Stefan Wagner ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a new tool for your toolbox: "javap", which comes with the JDK. "javap -c" disassembles the bytecode of a class; very handy for seeing how language features are implemented, and for comparing your options. OK, so here's one version:


This disassembles to


And here's the other version:


and its disassembly:


Identical!

The difference you see in the hexdumps is almost certainly the line number table: a class file can contain a table that relates bytecode indexes to source code line numbers, and that table would differ for these two classes.

[ EJFH: Edited to disable smilies! ]
[ May 13, 2005: Message edited by: Ernest Friedman-Hill ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well - depending on what I do inside the loop, I get identical code or not.
And not being able to interpret the output of javap -c, I can't say whether it is functionally identic bytecode.

But I believe you're right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!