Thread: Global/Static function/variable slower to access?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    31

    Global/Static function/variable slower to access?

    Hey guys,

    So I learned about the cpu and some real low level stuff recently and I have to ask, are global/static variables or functions slower to access/call?

    The reasoning may be because that they are outside the current scope and would not be found in the registers or L1 cache and would probably located in the RAM.

    I hope I am making some sense.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I think you would have to examine things on a case by case basis.

    On systems where stack space is dynamically mapped, the first time you access a local variable may result in a page fault, which would be exceedingly slow.

    Likewise, an often accessed global variable may be "permanently" in cache.

    But then again, on a paged virtual desktop OS, the whole thing could be being paged in and out of memory anyway.

    The difference is minute (and could go one way or the other). And being so small, it's effect will be swamped by all sorts of other factors, some under your control (like using the optimisation flags), and some not under your control (like how many other processes are running on the machine).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  2. Replies: 8
    Last Post: 09-27-2010, 04:11 PM
  3. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  4. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM