I have 5 functions which gets called 10000+ times (on an average). All of them modifies/uses certain variables.
I know it is bad practice to have global variables. But for performance sake, does it make sense to keep them global and not pass them around - specially when I am making function call these many times?
OR I am not going to gain much in terms of performance?
The first is that allocating global variables may be faster, since they are only allocated once, at the time the program is first spawned, whereas local variables must be allocated every time a function is called.
Coalescing global variables causes variables that are frequently used together to be mapped close together in memory. This strategy improves performance in the same way that changing external variables to static variables does.
A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code. Using global variables causes namespace pollution.
Global variables can be accessed by all the functions present in the program. Only a one-time declaration is required. Global variables are very useful if all the functions are accessing the same data.
Do not introduce global variables/global state for performance purposes. This is misguided, contrary to all good coding practices, and usually will not help performance (it might even hurt).
If you're finding it too costly to pass around lots of variables, you can put them all in a context struct
and pass a single pointer to the struct
. This way you avoid creating global state (even static
storage duration variables are global state) which prevents your code from being usable in multiple instances. The cost is virtually zero, and in fact it will be less costly than global variables in position-independent code (shared libraries or position-independent executables).
You may see a small performance improvement by reducing the number of parameters that are passed to your functions, by pre-allocating variables (eg. global or static variables)
The change in performance is absolutely going to depend on a number of things, not the least of which is the platform that you're developing on. If you're developing for a tiny microprocessor the time taken to copy parameters onto the stack (from the calling function) and the time taken to access the stack may be a significant enough proportion of the total execution time to warrant it.
Note that in the situation where the time taken to pass parameters is significant you may find that some of the other suggestions (eg. passing pointers to structures, passing pointers to static variables) will not provide any benefit either. Using global variables does give the compiler/linker the opportunity to hard code access to those variables rather than having to access them indirectly from a pointer on the stack. This is particularly pertinent to processors that don't have any cache.
Of course this is all very target dependent, and highly dependent on the instruction set of the processor that you're using. On any platform with a reasonable instruction set you should see an improvement.
However, measures like this should only be taken after profiling this code. On most platforms, with any non-trivial function, the time taken to pass the parameters and access them is insignificant. Any potential performance gain would come at the cost of more difficult maintenance of the code.
It is very likely that you would achieve greater performance gains by using other optimisation techniques. Check this question for some methods to try.
Edit: I see from one of your comments that you are still in the design phase of this project.
It is too early in the process to be making optimisations like this. At this stage you'll have a far greater impact on performance by optimising the algorithms you use than minimising at the instruction level like this.
static variables is C have file scope, and they might be sufficient in your case - provided you can group your functions into 1 file. And for me static variables are several orders of magnitude less problematic than globals.
One often overlooked issue is that variables declared inside a function body will be allocated on the stack, whereas static variables are typically allocated from a less confined memory pool called bss. So, having all variables neatly defined inside functions can lead to stack exhaustion problems, and that can be avoided in a quite clean way with statics.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With