Forums > Java

Little efficiency question

Sep 09 2010 | 9:06 am

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 ?


Sep 11 2010 | 12:08 am

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.

Sep 11 2010 | 7:38 am

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).

Sep 11 2010 | 10:31 pm

Thanks for your advices !

Viewing 4 posts - 1 through 4 (of 4 total)

Forums > Java