c++ - Performance: should I use a global variable in a function which gets called often? -
First of all, I should get this fact from my chest that I am trying to do the right thing I am in contradiction which is now the right way to the right way.
I am modifying a driver for a peripheral which includes the function - call it Send ()
. I have a timestamp variable in the function, so function loop for a particular time.
So, should I declare variable global (in this way it is always in memory and is lost every time I run the function) or should I declare the function reference (and in the reference context Leave local)
Please note that the function can be called multiple times per millisecond.
The speed of execution for local versus global variables should not be quite different. The only real difference is where the variable remains. Local variables are allocated on the stack, global variables are in a separate memory segment. It is true that every time you log in regularly, local variables are allocated, but allocation of memory to move the stack indicator is a single instruction.
If the variable should be global or not, local.
Comments
Post a Comment