Home AMX User Forum NetLinx Studio

Is stack_var quicker than global?

If you are executing a loop such as:
		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

Comments

  • a_riot42a_riot42 Posts: 1,624
    Spire_Jeff wrote: »
    Does putting the global variable into a stack_var speed up execution? Jeff

    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
  • Spire_JeffSpire_Jeff Posts: 1,917
    The example code is derived from a real world example, but the question is more theoretical than practical.

    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.
  • Joe HebertJoe Hebert Posts: 2,159
    Is a stack var quicker than a global? I don?t know but it might be a fun race to watch.
  • DHawthorneDHawthorne Posts: 4,584
    I would imagine that if the stack has to be accessed a great many times, there would be some slowdown, but I doubt with the speed of processors and memory allocations in general that it could possibly matter. Memory allocation has to be the least processor-intense operation out there. Physical speed of the memory is more of a factor, but again, I doubt it really matters.

    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.
Sign In or Register to comment.