Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Does the static variable modifier make variable access faster?

Does the static variable modifier make variable access faster?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hello, Is an access to a variable with static modifier faster than a normal variable? I have a recursive function:

    void recursiveSolve(char *pszInput, int nItems)
    {
    int pArray[10];
    // Do some stuff...
    }

    The initial contents of pArray doesn't matter, it gets overwritten in all cases. If I declare pArray as static int[], would the recursive function be faster? I don't know, perhaps a static variable has a fixed position in memory and doesn't get allocated again and again when the function calls itself recursively. Do you know? -Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

    F A B 3 Replies Last reply
    0
    • D Dominik Reichl

      Hello, Is an access to a variable with static modifier faster than a normal variable? I have a recursive function:

      void recursiveSolve(char *pszInput, int nItems)
      {
      int pArray[10];
      // Do some stuff...
      }

      The initial contents of pArray doesn't matter, it gets overwritten in all cases. If I declare pArray as static int[], would the recursive function be faster? I don't know, perhaps a static variable has a fixed position in memory and doesn't get allocated again and again when the function calls itself recursively. Do you know? -Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

      F Offline
      F Offline
      FlyingDancer
      wrote on last edited by
      #2

      I think so. Because a saying said that the more space, the less time. When stored as static varibles the more space is needed (the none-static varibles will be freed when they won't be needed), so I think the function needs less time to run. Do you think I am right, don't you? LeonOrient

      D 1 Reply Last reply
      0
      • F FlyingDancer

        I think so. Because a saying said that the more space, the less time. When stored as static varibles the more space is needed (the none-static varibles will be freed when they won't be needed), so I think the function needs less time to run. Do you think I am right, don't you? LeonOrient

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #3

        I don't know if that saying applies to this problem ;) -Dominik


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

        1 Reply Last reply
        0
        • D Dominik Reichl

          Hello, Is an access to a variable with static modifier faster than a normal variable? I have a recursive function:

          void recursiveSolve(char *pszInput, int nItems)
          {
          int pArray[10];
          // Do some stuff...
          }

          The initial contents of pArray doesn't matter, it gets overwritten in all cases. If I declare pArray as static int[], would the recursive function be faster? I don't know, perhaps a static variable has a fixed position in memory and doesn't get allocated again and again when the function calls itself recursively. Do you know? -Dominik


          _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

          A Offline
          A Offline
          Andrew Walker
          wrote on last edited by
          #4

          It shouldn't matter. It would be better to try and optimise using a better algorithm than changing to static variables Using static variables is a C concept rather than a C++ concept. It can be very problematic if you have to port the code into a DLL (look at the MSVC6 STL problems with DLL's for the perfect example)

          B 1 Reply Last reply
          0
          • D Dominik Reichl

            Hello, Is an access to a variable with static modifier faster than a normal variable? I have a recursive function:

            void recursiveSolve(char *pszInput, int nItems)
            {
            int pArray[10];
            // Do some stuff...
            }

            The initial contents of pArray doesn't matter, it gets overwritten in all cases. If I declare pArray as static int[], would the recursive function be faster? I don't know, perhaps a static variable has a fixed position in memory and doesn't get allocated again and again when the function calls itself recursively. Do you know? -Dominik


            _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

            B Offline
            B Offline
            BhaskarBora
            wrote on last edited by
            #5

            hi there .. u r right partly... all static variables (no matter where they are defined) are not created on stack like local vars. and also static vars are created once for a process/program and remain till the end. this is the reason when your function has static var array defined rather than a local var array, the execution is faster. as static var is created during the program startup and in your case not everytime when you invoke the function (recursive or otherwise). if it would have been a local array then execution would have been little slower as compare to the static array version (as OS will allocate the array on stack everytime you call the function and removes it from stack when function returns). static var array is shared in all calls to the function. Also, there is a wastage of memory when you are not using the array. to over come this, you can define a 'int pointer' as static and allocate the memory when you need to use the array. when you no longer want the array, relase the allocated memory. this would be a smart solution ! "Think big, think fast, think ahead. Ideas are no one's monopoly"

            D 1 Reply Last reply
            0
            • A Andrew Walker

              It shouldn't matter. It would be better to try and optimise using a better algorithm than changing to static variables Using static variables is a C concept rather than a C++ concept. It can be very problematic if you have to port the code into a DLL (look at the MSVC6 STL problems with DLL's for the perfect example)

              B Offline
              B Offline
              BhaskarBora
              wrote on last edited by
              #6

              i am sorry my friend... static variables is not only a concept in C but it is extensibly used in C++ too. infact "static var" concept is not confined to any language or OS. its a generic concept, which is used in many languages and operating systems. there are some solutions, which are best implemented with static concept. "Think big, think fast, think ahead. Ideas are no one's monopoly"

              A 1 Reply Last reply
              0
              • B BhaskarBora

                i am sorry my friend... static variables is not only a concept in C but it is extensibly used in C++ too. infact "static var" concept is not confined to any language or OS. its a generic concept, which is used in many languages and operating systems. there are some solutions, which are best implemented with static concept. "Think big, think fast, think ahead. Ideas are no one's monopoly"

                A Offline
                A Offline
                Andrew Walker
                wrote on last edited by
                #7

                BhaskarBora wrote: static variables is not only a concept in C but it is extensibly used in C++ Perhaps I worded it badly. But being able to do it doesn't mean you should. If you have to do something like this why are there so many discussion about the 'meyers singleton' pattern and order of destruction. Dynamic allocation of memory through a static pointer can be walking a dangerous road. Especially if you need to control the order of destruction.

                B 1 Reply Last reply
                0
                • B BhaskarBora

                  hi there .. u r right partly... all static variables (no matter where they are defined) are not created on stack like local vars. and also static vars are created once for a process/program and remain till the end. this is the reason when your function has static var array defined rather than a local var array, the execution is faster. as static var is created during the program startup and in your case not everytime when you invoke the function (recursive or otherwise). if it would have been a local array then execution would have been little slower as compare to the static array version (as OS will allocate the array on stack everytime you call the function and removes it from stack when function returns). static var array is shared in all calls to the function. Also, there is a wastage of memory when you are not using the array. to over come this, you can define a 'int pointer' as static and allocate the memory when you need to use the array. when you no longer want the array, relase the allocated memory. this would be a smart solution ! "Think big, think fast, think ahead. Ideas are no one's monopoly"

                  D Offline
                  D Offline
                  Dominik Reichl
                  wrote on last edited by
                  #8

                  Thanks! I will try the static pointer idea! -Dominik


                  _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;)

                  1 Reply Last reply
                  0
                  • A Andrew Walker

                    BhaskarBora wrote: static variables is not only a concept in C but it is extensibly used in C++ Perhaps I worded it badly. But being able to do it doesn't mean you should. If you have to do something like this why are there so many discussion about the 'meyers singleton' pattern and order of destruction. Dynamic allocation of memory through a static pointer can be walking a dangerous road. Especially if you need to control the order of destruction.

                    B Offline
                    B Offline
                    BhaskarBora
                    wrote on last edited by
                    #9

                    dear friend ! its up to you how you perceive things ! "Think big, think fast, think ahead. Ideas are no one's monopoly"

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups