Question

I have a draw method that will be called repeatedly (every frame...). Inside this method I have a handful of locally defined variables (roughly 20). I am considering putting these inside a structure, and passing a pointer to that structure as a parameter to the method. This is in hopes that the indirection operations are faster than placing the variables on the stack.

Is there, in general, a reasonable performance difference here?

Was it helpful?

Solution

Placing variables on the stack (on x86 processors, at least) is very fast. On an x86 processor, using the stack amounts to subtracting once at the beginning of the function and adding once at the end; adding more variables just changes the number and will not make it any slower. If you're using indirection, that might be a few more operations, so it might be a bit slower, but if your compiler puts that struct pointer in a register, it'll probably be just as fast loading and storing.

I'd stick with whatever makes your code simpler, as it's a micro-optimization anyway, and you should only consider it after you've profiled and determined it's a bottleneck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top