Hi there. I've written some code that frequently accesses a dynamically created object during a loop structure. My problem is that I've noticed that accessing fields within
my object is significantly slower than accessing stack memory.
For example, if I benchmark the duration of the following code:
float x = 0.0f; for (int i = 0; i < 1000000; i++) x = 0.5f * 0.5f;
It is way quicker than this (assume 'val' is a float defined within the object 'x'):
FloatObject x = new FloatObject(); for (int i = 0; i < 1000000; i++) x.val = 0.5f * 0.5f;
I won't deny for a moment that I'm a noob with Java. I believe the latter phenomenon is due to the JIT "finding" the object x in memory every time it is called upon, or, perhaps
something to do with the garbage collector.
My question is: Is there any way to speed up access of heap data? I realize that the stack will always be faster, but I'd like to know if there's a compiler argument
that might allow faster access to heap data.
Thanks for your time.