Is stack_var quicker than global?
Spire_Jeff
Posts: 1,917
If you are executing a loop such as:
Does putting the global variable into a stack_var speed up execution? I am not doing this for speed, but for readability and some code related changes that I don't want changing the global. I was just thinking that since stack memory should be quicker to access, it may speed up execution.
When I have a few spare moments, I might setup a benchmark test, but I figured I would throw it to the forum for discussion and speculation for the time being
Jeff
STACK_VAR INTEGER x STACK_VAR INTEGER nDEV_CNT; STACK_VAR INTEGER CUR_RM CUR_RM = nCURRENT_ROOM[TPINDEX] WHILE(uROOM[CUR_RM].nDEVICE_MUSIC[x]){ nDEV_CNT++; x++; }
Does putting the global variable into a stack_var speed up execution? I am not doing this for speed, but for readability and some code related changes that I don't want changing the global. I was just thinking that since stack memory should be quicker to access, it may speed up execution.
When I have a few spare moments, I might setup a benchmark test, but I figured I would throw it to the forum for discussion and speculation for the time being
Jeff
0
Comments
I really can't see how it could be faster, but even if it was by some small margin, I can't see it having any effect on the final program speed or latency. Having a variable called CUR_RM and another called nCURRENT_ROOM is more confusing than enlightening to me. Is this a real world example? That while loop looks like it may never end except on an array out of bounds error.
Paul
The While loop in actual code is guaranteed to fail within a small set of executions.
The confusion should not be a problem as in my mind, this would be a function call and the stack_var would only be used within the function. The only time I could see this actually being beneficial is if there is a very large number of iterations, but as of right now, I cannot think of any situations... hence the theoretical nature of the topic. Sorry for not being clear about the theoretical nature of this question.
Jeff
P.S.
I know that there are certain operations that are quicker using a stack_var than a volatile. I did some benchmarks and using stack_vars did show improvement in a lot of situations with loops and especially with strings as I recall.
This is one of those cases I would go for readability and the convenience of limited scope in favor of a minuscule and negligible potential speed increase.