Performance question: array lookup vs. local vars in memory?

Hi there,

I'm working on my first game and I'm wondering about best practice for targeting low end machines. I know from programming other languages that assigning a regularly accessed array value to a local variable gives you a slight performance boost. But I notice that the memory usage goes up when I do this.

So my question is: what is more valuable to the performance? Avoiding multiple array lookups by creating the local var? Or using up some of the memory by creating local variables? Unless I've misunderstood how this works. It would be great to get another opinion on this.

Many thanks,
Lance

Generally speaking, just do what makes your code easier to write/maintain and don't worry about it. Most of the time, you won't be able to tell the difference. Performance bottlenecks are likely going to be doing real work like physics and drawing and the time to look up an array will be insignificant. Memory usage will also be dominated by sounds and graphics and the memory required for another variable is insignificant.

The exceptions are if you have a tight inner loop that you are calling a lot (say thousands of times per frame) where faster look up might make a difference. Or if you exceed the maximum number of local variables in a scope (I think the number is around 200).

views:1468 update:2011/9/26 8:07:09
corona forums © 2003-2011