Efficeincy Question

If there is a function that renders an object, these objects are medium sized in terms of memory and process time, the first rendered object is in the background, and you want to automate the layering, is it more efficient in terms of memory to
1 have an array of objects that is cycled through with a simple while loop
2 have an array of objects that is iterated through with a pointer
3 it doesn't matter
I'm not quite sure which one is best. 2 seems much more reusable though.
I'd like the code to be reusable, but not a memory hog.
Generally the difference is negligible, though depending on your implementation, one may have a slight advantage over the other.

If you don't need to preserve the original values of the arguments in your function, then 2 has an advantage over 1, as you can increment the pointer to the first element of the array that was (likely) passed as an argument until you either find a terminating element or until you've incremented it a number of times equal to the array's size. In the latter case, you can simply decrement the size variable that was also passed in until it reaches 0 (beware of off-by-ones).

Otherwise, 1 has an advantage over 2, unless your array will be larger element-wise than the maximum value of an integer half as large as a pointer (4,294,967,295 elements on 64-bit systems and 65,535 elements on 32-bit systems), on account of your ability to choose counter variable types that are smaller than pointers.

-Albatross
Last edited on
Topic archived. No new replies allowed.