Little efficiency question

Alexandre's icon

Hi,
Let's say i'm calling sequentially a calculation_method() nearly 80000 times per seconds.

While coding this, all my floats variables that are used in this calculation_method(), i'm declaring them in the class, in order to not surcharge the garbage collector. Am i right ?
Or should i declare my floats variables inside my calculation_method() ?
In Java, can sometimes local variable run a bit faster than global variables or never ?
Is there something to worry about or not ?

Thanks,

Hans Höglund's icon

The GC has nothing to do with local primitives, they are allocated on the operand stack. The role of the GC is to clear up classes and arrays that are no longer referenced.

Locals should be prefered to static fields in any case. They are simpler, safer and probably more viable to optimization.

Roth's icon

Yeah, even though you are saving a declaration by declaring an instance or static variable from what I've read it can take 2-3 times as long to access and modify that variable versus using a local variable. One thing you can look out for optimizing is avoiding declaring local variables within loops (probably obvious if you are asking this question).

Alexandre's icon

Thanks for your advices !