views:

35

answers:

1

Is someArray[index] a faster way to get to a value than someObject.field?

E.G.:

if(intArray[i] == 42) {//do stuff}

VS

if(someObject.x == 42) {//do stuff}

I will try to test it soon and post the results; just wondering if you all had any thoughts.

+1  A: 

I would expect the using the array element to be a lot quicker since the reference points directly to the value to be retrieved.

When you reference a member on an Object field, the runtime has to determine which class the member belongs to and so on, and the process will take longer.

Dave Webb
Thanks, reason I ask is because I use a lot of Vector objects (they only wrap a X and Y coordinate) so they could easily be represented by int[2] (using fixed point). I will try to profile some code soon'ish.
Michiel
My tests show that it is faster; around 10ms on 100.000 iterations.
Michiel