andybiochem Posted December 21, 2009 Share Posted December 21, 2009 Hi! Very excited to be using the new AI v3.3.2.0!!!! wooo! Also VERY happy with the inclusion of 'static' declarations, this opens up a whole new world for me! However, having tested STATIC with a couple of functions, it seems I'm getting a big performance hit compared to declaring variables as global. I was hoping someone could explain where I was going wrong. Example: A while back I wrote a function to generate normally distributed random numbers. Each time the function is ran, it actually generates two random numbers, so I designed a way of taking advantage of the second number generated by using global variables which check to see if the function was returning a 'fresh' number, or the second of a pair of numbers (erm, that sounds more complicated than it actually is). Anyway, by shifting the 'global' variables into the function by using 'static', I get a massive performance hit. expandcollapse popupGlobal $bPass2 = False Global $iX, $iY, $iR ;----- number of iterations ----- $n = 1000000 ;----- test GLOBAL version of function ----- $timer = TimerInit() For $i = 1 to $n _Random_Gaussian_G(0,1) Next ConsoleWrite("GLOBAL = " & TimerDiff($timer) / 1000 & @CRLF) ;----- test STATIC version of function ----- $timer = TimerInit() For $i = 1 to $n _Random_Gaussian_S(0,1) Next ConsoleWrite("STATIC = " & TimerDiff($timer) / 1000 & @CRLF) ;----- GLOBAL version of function ----- Func _Random_Gaussian_G($iMean,$iSD) Switch $bPass2 Case True $bPass2 = False Return (($iY * (Sqrt((-2 * (Log($iR) / $iR))))) * $iSD) + $iMean Case False Do $iX = ((2 * Random()) - 1) $iY = ((2 * Random()) - 1) $iR = ($iX^2) + ($iY^2) Until $iR < 1 $bPass2 = True Return (($iX * (Sqrt((-2 * (Log($iR) / $iR))))) * $iSD) + $iMean EndSwitch EndFunc ;----- STATIC version of function ----- Func _Random_Gaussian_S($iMean,$iSD) Static $sbPass = False Static $siX, $siY, $siR Switch $sbPass Case True $sbPass = False Return (($siY * (Sqrt((-2 * (Log($siR) / $siR))))) * $iSD) + $iMean Case False Do $siX = ((2 * Random()) - 1) $siY = ((2 * Random()) - 1) $siR = ($siX^2) + ($siY^2) Until $siR < 1 $sbPass = True Return (($siX * (Sqrt((-2 * (Log($siR) / $siR))))) * $iSD) + $iMean EndSwitch EndFunc For 1,000,000 iterations: GLOBAL = 23 seconds STATIC = 33 seconds Why the huge difference? Am I using static incorrectly? - Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 21, 2009 Moderators Share Posted December 21, 2009 andybiochem,I imagine it is because the STATIC keyword has to be interpreted each time. You get a similar performance hit if you declare a LOCAL variable in a loop as shown in this particularly useless piece of code: $iBegin = TimerInit() Func_Once() ConsoleWrite(TimerDiff($iBegin) & @CRLF) $iBegin = TimerInit() Func_Each() ConsoleWrite(TimerDiff($iBegin) & @CRLF) Exit Func Func_Once() Local $iFred For $n = 1 To 10000 $iFred = $n Next EndFunc Func Func_Each() For $n = 1 To 10000 Local $iFred = $n Next EndFuncBut no doubt a Dev will be along shortly to give chapter and verse. M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
andybiochem Posted December 21, 2009 Author Share Posted December 21, 2009 andybiochem,I imagine it is because the STATIC keyword has to be interpreted each time.M23Thanks for the reply!Hmmm, I thought the whole point with static was that it's only interpretted ONCE, the first time the function is called?Perhaps I'm not understanding static correctly? - Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar! Link to comment Share on other sites More sharing options...
monoceres Posted December 21, 2009 Share Posted December 21, 2009 Thanks for the reply!Hmmm, I thought the whole point with static was that it's only interpretted ONCE, the first time the function is called?Perhaps I'm not understanding static correctly?It will be executed once, but autoit will still find the line every time the function is called, extract the variable name and do a search to see if this variable has already been declared (if it has then nothing more will happen). Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
andybiochem Posted December 21, 2009 Author Share Posted December 21, 2009 It will be executed once, but autoit will still find the line every time the function is called, extract the variable name and do a search to see if this variable has already been declared (if it has then nothing more will happen).Ah, ok. I imagined that autoit would simply ignore any declarations after the first one.Just out of interest, is this an anomoly of AutoIt or would this happen in c++ too? I imagine there's an awful lot of these searches/checks going on in the background of AutoIt that wouldn't occurr in c.I'll just stick with the Global method for now, the static implementation was simply cosmetic anyway.Thanks guys! - Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar! Link to comment Share on other sites More sharing options...
monoceres Posted December 21, 2009 Share Posted December 21, 2009 Just out of interest, is this an anomoly of AutoIt or would this happen in c++ too? I imagine there's an awful lot of these searches/checks going on in the background of AutoIt that wouldn't occurr in c.These things do not happen in compiled languages. Regular variables for example are stack based and therefore in the compiled code the variable is just a stack offset. Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now