ErosOlmi
05-08-2008, 08:29
The STATIC statement is usually present inside a Sub or Function and is used to define variables that retain their values even after the Sub or Function ends.
A static variable is local to its Sub or Function, and can have the same name as other variables in other parts of the script without conflict.
STATIC variable[()] [, variable[()]] [, ...] AS type [= anyValue]
So, the main characteristic of STATIC variables is that they retain their values as long as the script is running. But if they retain their value it means they are just allocated once even if the function in which they are defined is executed more than once. In few words it means that STATIC ... statement is executed by thinBasic only the very first time it is encountered. In all other cases it is just ignored.
Ok, I've got it. So?
The little detail that STATIC ... is executed only the very first time it is encountered have great impact on script execution. One of the more resource demanding into a process is memory allocation/deallocation. Now think a function having just one local variable defined inside it but executed 1 million times:
FUNCTION MyFunction() AS LONG
LOCAL MyVar AS LONG
'---Do something with MyVar than return its value
MyVar = 123 + 456
FUNCTION = MyVAr
END FUNCTION
Think for a second to call MyFunction 1 million times. What will happen is that 1 million times MyVar will be created into a local stack and again 1 million times MyVar will be deallocated from the local stack when function ends.
Now, rewrite the same function chaging just LOCAL to STATIC:
FUNCTION MyFunction() AS LONG
STATIC MyVar AS LONG
'---Do something with MyVar than return its value
MyVar = 123 + 456
FUNCTION = MyVAr
END FUNCTION
What will happen if we call MyFunction 1 million times but now having the same variable defined with STATIC statement?
MyVar will be allocated just once and never deallocated till the script ends its execution. This have great great impact on the execution speed.
Just for fun, try to execute the above 2 versions of MyFunction in a 1 million loop and see the difference. Let me know about it.
When STATIC variables can be a boomerang?
All possible problems about STATIC variables are related to the fact that they retain their values. So:
in recursive functions STATIC variables will retain their values. During recursion this can be a problem if you are not aware of it
in case of strings, arrays or complex structures, if big amount of data is allocated into a STATIC variable, it will remain allocated for the duration of the script or till less data will be assigned again to the same STATIC variable.
A static variable is local to its Sub or Function, and can have the same name as other variables in other parts of the script without conflict.
STATIC variable[()] [, variable[()]] [, ...] AS type [= anyValue]
So, the main characteristic of STATIC variables is that they retain their values as long as the script is running. But if they retain their value it means they are just allocated once even if the function in which they are defined is executed more than once. In few words it means that STATIC ... statement is executed by thinBasic only the very first time it is encountered. In all other cases it is just ignored.
Ok, I've got it. So?
The little detail that STATIC ... is executed only the very first time it is encountered have great impact on script execution. One of the more resource demanding into a process is memory allocation/deallocation. Now think a function having just one local variable defined inside it but executed 1 million times:
FUNCTION MyFunction() AS LONG
LOCAL MyVar AS LONG
'---Do something with MyVar than return its value
MyVar = 123 + 456
FUNCTION = MyVAr
END FUNCTION
Think for a second to call MyFunction 1 million times. What will happen is that 1 million times MyVar will be created into a local stack and again 1 million times MyVar will be deallocated from the local stack when function ends.
Now, rewrite the same function chaging just LOCAL to STATIC:
FUNCTION MyFunction() AS LONG
STATIC MyVar AS LONG
'---Do something with MyVar than return its value
MyVar = 123 + 456
FUNCTION = MyVAr
END FUNCTION
What will happen if we call MyFunction 1 million times but now having the same variable defined with STATIC statement?
MyVar will be allocated just once and never deallocated till the script ends its execution. This have great great impact on the execution speed.
Just for fun, try to execute the above 2 versions of MyFunction in a 1 million loop and see the difference. Let me know about it.
When STATIC variables can be a boomerang?
All possible problems about STATIC variables are related to the fact that they retain their values. So:
in recursive functions STATIC variables will retain their values. During recursion this can be a problem if you are not aware of it
in case of strings, arrays or complex structures, if big amount of data is allocated into a STATIC variable, it will remain allocated for the duration of the script or till less data will be assigned again to the same STATIC variable.