Re: Microoptimization - variables inside or out of the loop block?
On Wed, 21 Oct 2009 14:10:50 +0200, Ivan Voras <ivoras@fer.hr> wrote,
quoted or indirectly quoted someone who said :
Hi,
Is there any point in trying to help the JVM by replacing this kind of code:
for (i = 0; i < a_lot; i++) {
doSomething();
LargeObject o = getNextLargeObject();
doSomethingElse(o);
...
}
with:
LargeObject o;
for (i = 0; i < a_lot; i++) {
doSomething();
o = getNextLargeObject();
doSomethingElse(o);
...
}
see http://mindprod.com/jgloss/disassembler.html
To see if there is any difference at all in the generated byte. A
local variable declaration without assignment generates no code.
I suspect you won't find any difference.
So you might as well reduce the scope of any variable to the minimum
since that makes for the most maintainable code.
I would further by interested to see how clever GC is about local
variables. I suspect stack slots pointing to objects are not GCed,
even if the variable associate with the stack slot has officially gone
out of scope.
If it turns out my guess is right, it means that nullifying unused
local variables is a good idea if the method is not going to terminate
very soon.
If I am wrong, then I would like to understand how GC figures out
which stack slots contain live references.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Nothing is so good as it seems beforehand.
~ George Eliot (born: 1819-11-22 died: 1880-12-22 at age: 61) (Mary Ann Evans)