Performance: Should a global variable be used in a function called frequently?

First of all, let me get from my chest the fact that I am a greenhorn trying to do something right, which means that from time to time I get a contradiction about what is right.

I change the driver for the periphery, which contains a function - lets call it Send() . In the function, I have a timestamp variable, so the function cycle for a certain period of time.

So, should you declare a global variable (that way, it is always in memory and no time is wasted declaring it every time the function starts) or do I leave the variable local in the context of the function (and avoid the bad design of the template with global variables)?

Please keep in mind that a function can be called several times in a millisecond.

+6
source share
4 answers

The execution speed should not be significantly different for local and global variables. The only real difference is where the variable lives. Local variables are allocated on the stack, global variables are in a different memory segment. It is true that local variables are allocated every time you enter a procedure, but memory allocation is one command to move the stack pointer.

When deciding whether a variable should be global or local, there are much more important considerations.

+5
source

When implementing the driver, try to avoid global variables as much as possible, because:

  • They are thread-safe and you have no idea about the user application scheduling scheme (in fact, even without threads, using multiple instances of the same driver is a potential problem).

  • It automatically enables the creation of a data section as part of the executable image of any application that references your driver (something that a software programmer might require).

+4
source

Have you profiled a fully optimized version of the code release and identified a bottleneck as small distributions in this function?

The change you are proposing is micro-optimization; Going to a small part of your code with the intention of making it more efficient. If the question to the above question is β€œno,” as I expected, you should not even think about such things.

Choose the right algorithm for your code. Write your code using idiomatic methods. Do not write in micro-optimization. You may be wondering how good your compiler is for optimizing your code for you. He will often be able to optimize these small allocations, but even if he cannot still not know whether the performance penalty imposed by them is noticeable or significant.

+2
source

For drivers, usually with an independent position, global variables are available indirectly with the GOT table if operations with IP-relative are available (i.e. x86_64, ARM, etc.)

In the case of GOT, you can consider this an additional indirect pointer.

However, even with an additional pointer, it will not have a noticeable difference if it is β€œonly” called in the mill frequency.

0
source

Source: https://habr.com/ru/post/973224/


All Articles