Sign in to follow this  
Followers 0
andybiochem

STATIC performance hit?

6 posts in this topic

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.

Global $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!

Share this post


Link to post
Share on other sites



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: :evil:

$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

EndFunc

But 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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

andybiochem,

I imagine it is because the STATIC keyword has to be interpreted each time.

M23

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?


- 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!

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0