PDA

View Full Version : Do you know: STATIC variables



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.

Petr Schreiber
05-08-2008, 08:37
Hi Eros,

I never thought about statics this way, they really bring significant speed gain.
On my PC the static powered function executed 1.9x faster than local variable based one.


Petr

ErosOlmi
05-08-2008, 09:06
Execution speed difference can be little or big depending on your hardware config.
In all cases where processor goes out and have to talk with memory or other hardware parts we cannot predict.

For example on my PC a function that takes 9.8 secs to execute with LOCALs went down to 3.8 with STATICs without changing anything else. The more LOCAL can be used as STATIC the better it is.

This let me also think that we can improve a lot general thinBasic execution speed. I have already some ideas about how to achieve better performances but it will mean rewrite a good part of Core engine. Not worth to do it now but in future releases and not before we will finish support for Win9x system.

Ciao
Eros

ErosOlmi
05-08-2008, 09:25
I never thought about statics this way, they really bring significant speed gain.


One of the reasons why I opened this new "Do you know ..." forum is to give little snapshots about little but important specific aspects that get lost in the big amount of thinBasic possibilities. Once this forum will have significant number of "Do you know ..." posts, I will use them as a sort of thinBasic advertising area when someone ask about thinBasic in other programming forums.

Ciao
Eros

Jophur
05-08-2008, 09:47
Thank you.

This is just the kind of feedback that is extremely helpful for beginners in thinBacis like me. I know the problems with allocating and freeing memory in older languages, but it is so easy to miss with modern system.

/Kent

ErosOlmi
05-08-2008, 11:03
Thanks Kent.

It is pleasure to see those posts can help.