C++ performance of global variables

前端 未结 9 1777
谎友^
谎友^ 2021-01-04 07:32

For clarification: I know how evil globals are and when not to use them :)

  • Is there any performance penalty when accessing/setting a global variable vs. a loca
相关标签:
9条回答
  • 2021-01-04 07:41

    There are a number of compiler optimisations that are possible with local variables but not with global variables, so in some cases you might see a difference in performance. I doubt that your global variable is being accessed in a performance-critical loop though (very bad design if it is !) so it's probably not going to be an issue.

    0 讨论(0)
  • 2021-01-04 07:42

    The answer is tied to the overall structure of the program.

    For example, I just disassembled this, and in both cases the looping variable was moved into a register, after which there was no difference:

    int n = 9;
    int main()
    {
        for (n = 0; n < 10; ++n)
            printf("%d", n);
    
        for (int r = 0; r < 10; ++r)
            printf("%d", r);
    
        return 0;
    }
    

    Just to be sure, I did similar things with classes and again saw no difference. But if the global is in a different compilation unit that might change.

    0 讨论(0)
  • 2021-01-04 07:49

    There is no performance penalty, either way, that you should be concerned about. In adition to what everyone else has said, you should also bear in mind the paging overhead. Local instance variables are fetched from the object structure which has likely (?) already been paged into cache memory). Global variables, on the other hand, may cause a different pattern of virtual memory paging.

    Again, the performance is really not worth any consideration on your part.

    0 讨论(0)
  • 2021-01-04 07:55

    It's more of the way how you use data stored in your variables that matters performance wise then how you declare them. I'm not sure about the correct terminology here, but one can define two types of data access. Shared access (where you access same data from different parts of the code) and private data, where each part has its own data. By default global variables imply shared access and local imply private access. But both types of access can be achieved with both types of variables (i.e. local pointers pointing to the same chunk of memory, or global array where each part of code access different part of array).

    Shared access has better caching, lower memory footprint, but is harder to optimize, especially in multi threaded environment. It is also bad for scaling especially with NUMA architecture..

    Private access is easier to optimise and better for scaling. Problems with private access usually exist in situation where you have multiple copies of same data. The problems usually associated with these scenario's are higher memory footprint, synchronization between copies, worse caching etc.

    0 讨论(0)
  • 2021-01-04 07:57

    That would depend entirely on your machine architecture. Global variables are accessed via a single known address, whereas local variables are typically accessed by indexing off an address register. The chances of the difference between the two being significant is extremely remote, but if you think it will be important, you should write a test for your target architecture and measure the difference.

    0 讨论(0)
  • 2021-01-04 07:58

    Strictly speaking, no.

    Some things to consider: Global variables increase the static size of your program in memory. If access to the variable needs to be synchronized, that would incur some performance overhead.

    0 讨论(0)
提交回复
热议问题