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?
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
structand pass a single pointer to thestruct. This way you avoid creating global state (evenstaticstorage 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).