Little efficiency question

Sep 9, 2010 at 9:06am

Little efficiency question

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,

#52244
Sep 11, 2010 at 12:08am

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.

#187762
Sep 11, 2010 at 7:38am

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

#187763
Sep 11, 2010 at 10:31pm

Thanks for your advices !

#187764

You must be logged in to reply to this topic.