Does the static variable modifier make variable access faster?
-
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?? ;) -
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?? ;)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
-
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
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?? ;) -
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?? ;)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)
-
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?? ;)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"
-
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)
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"
-
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"
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.
-
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"
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?? ;) -
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.
dear friend ! its up to you how you perceive things ! "Think big, think fast, think ahead. Ideas are no one's monopoly"