Thread: static class variable vs. global variable

  1. #16
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I believe I understand most of the negative impact that global variables have on a program. One major issue is their negative effect on your program's performance.
    nadom7, yeah, it might be just a latency issue, though. Perhaps the program access local data quite a lot, but the global and static data that must stored in a different page it doesn't. In some situations, not using globals could be faster, but if you'll passing a lot of data round as parameters using globals could be faster. But I wouldn't worry too much about it. I'd just go for clarity first.


    Another issue with globals, I think, is a compiler that doesn't optimize outside of functions will have trouble optimizing somtimes. For example, say you have

    Code:
    int globalVar;
    
    void f()
    {
           int c = 0;
    
           globalVar ++;
           globalVar++;
           g()
    
           c = globalVar + 5;
    
           printf("c = %d\n", globalVar);
    }
    
    void g()
    {
           printf("global var = %d\n", globalVar);
    }
    Then, for such a compiler, it'll compile code to have to load globalVar into a register in f() before the call to g() but unless if the compiler can detect that g() doesn't modify globalVar, it'll likely have to reload globalVar into a register.



    Function algorithms are even more important in your program than I originally mentioned. If you are running the program on a PC such as Pentium, it still won't make much difference because of the CPUs very limited number of registers (there are only 4 general-purpose registers, eax ebx ecx and edx.
    Ancient Dragon, Pentium 4 has registers that are not visible to the assembly programmer. These latter pentiums are also RISC, and they, as I've been told, translate the very complex instructions into simple instructions that likely use hidden registers. They also have registers for floating point, operating system control, paging, MMX, etc so the number of registers in the latter processers has gone up quite a lot.

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    Thanks for all the input on this subject.

    okinrus: I believe you are correct in your example showing that the global will have to be reloaded.

    Ancient Dragon: You are correct in that I did not have my optimizations set correctly at all. After optimizing both of my example programs ( i believe correctly ) I received very different results. I was able to get the global var program's time down to 0.0078125 seconds! However the amazing thing is... after correctly optimizing the local var program it will only output 0 seconds! I have to add a few more function calls to the local var program to even get it output anything but 0 seconds. After adding two more functions I did get it to output 0.000315!!

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The optimizer may have made the function inline, then optimized the loop out altogether, but you'd have to see the assembly code to confirm.

  4. #19
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by Dante Shamest
    Global variables, as Stoned_Coder said, don't affect your program's performance.
    They're frowned upon because they tend to introduce name clashes and alot of coupling of functions that use that object. If you removed that global object later, all the functions that use it internally will have to have their definitions modified.
    I have had that problem you talk about. But I figured out it has to do with the way you asign your vars. Not the way you declare them. As an example, it happens a lot when ever I asign FALSE to an int var and there is a BOOL declared right under that area that the int was declared.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in pass a variable to a class
    By nima_pw in forum C# Programming
    Replies: 3
    Last Post: 06-09-2009, 07:30 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM