Jump to content
Sign in to follow this  

Performance Test - Declaring Variables

Recommended Posts


I wrote a benchmark script to measure variable declarations
to find out whether you should focus more on static or global variables

#cs ----------------------------------------------------------------------------

 AutoIt Version:

#ce ----------------------------------------------------------------------------

#Region Pre-Setting

Local $iTally1 = 0
Local $iTally2 = 0
Local $iTally3 = 0
Local $iTally4 = 0
Local $iTally5 = 0
Local $iTally6 = 0
Local $iTally7 = 0

Global $GLOBALCONST1 = 1
Global $GLOBALCONST2 = 1
Global $GLOBALCONST3 = 1
Global $GLOBALCONST4 = 1
Global $GLOBALCONST5 = 1

#EndRegion Pre-Setting

#Region Test Functions

Func s1()
    Static $i = $GLOBALCONST1
    Return $i

Func g2()
    Return $GLOBALCONST2

Func g3()
    Static $i7 = "gsdgdfegbgbrwefw"
    Return $GLOBALCONST3

Func g4()
    Static $i1 = 1
    Static $i2 = "asd"
    Static $i3 = 234
    Static $i4 = True
    Static $i5 = [0]
    Static $i6 = "hgsdg"
    Static $i7 = 1
    Static $i8 = 1
    Static $i9 = 1
    Static $i0 = 1
    Return $GLOBALCONST4

Func g5()
    Local $i = $GLOBALCONST5
    Return $i

Func g6()
    Local $i = 1
    Return $i

Func g7()
    Return 1

#EndRegion Test Functions

#Region Benchmark Loop

For $i = 0 To 15

    Local $tDelta = TimerInit()
        $iTally1 += s1()
    Until TimerDiff($tDelta) >= 1000

    Local $tDelta = TimerInit()
        $iTally2 += g2()
    Until TimerDiff($tDelta) >= 1000

    Local $tDelta = TimerInit()
        $iTally3 += g3()
    Until TimerDiff($tDelta) >= 1000

    Local $tDelta = TimerInit()
        $iTally4 += g4()
    Until TimerDiff($tDelta) >= 1000

    Local $tDelta = TimerInit()
        $iTally5 += g5()
    Until TimerDiff($tDelta) >= 1000

    Local $tDelta = TimerInit()
        $iTally6 += g6()
    Until TimerDiff($tDelta) >= 1000

    Local $tDelta = TimerInit()
        $iTally7 += g7()
    Until TimerDiff($tDelta) >= 1000


#EndRegion Benchmark Loop

ConsoleWrite(@CRLF&"Static1: "&$iTally1&" pkt"&@CRLF&"Global2: "&$iTally2&" pkt"&@CRLF&"Global3: "&$iTally3&" pkt"&@CRLF&"Global4: "&$iTally4&" pkt"&@CRLF&"Local5:  "&$iTally5&" pkt"&@CRLF&"Local6:  "&$iTally6&" pkt"&@CRLF&"Hardcode7:"&$iTally7&" pkt"&@CRLF)



Static1:   10291881 pkt     global to static
Global2:   13977324 pkt     only global
Global3:    9886169 pkt     global and static
Global4:    2933051 pkt     global and many statics
Local5:     9937314 pkt     global to local
Local6:    10306484 pkt     only local
Hardcode7: 14835319 pkt     no variable



100% no variable, hardcore value
94% only global variable use
69% only local variable use with hardcore value set
69% only static variable use with global variable value set
67% declaration of local variable with global variable value set
66% only global variable use with one static variable beside
20% only global variable use with ten static variables beside

My thesis of the result:

  • Be careful with declarations, whether local, global or static

Note: in my test the global variable performance was better than the local one, but in practice the global one would lose performance due to multiple operations


What is your best practice sharing data between multiple functions?

Share this post

Link to post
Share on other sites

I am quite new to autoit so I might not be using the best method but from my experience with 50K+ lines of excel files:
I declare all the variables I need in a local array, then I call the array in the function by using the Byref keyword so the function has acces to the original variables. However if the scope of a project is not much of an issue I would declare them Global.

For a script that reads out and processes .csv files with total size of approx 20MB and the arrays are declared locally it took  33.88 seconds

the same script with arrays declared globally took 34.61 seconds



Sorry for the doublepost, editing was not possible as far as I can tell and I accidently hit the submit reply button .

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By adjist
      Hello all! 
      Getting this error :
      (22) : ==> Variable used without being declared.: if $vNumber = 0 Then if ^ ERROR  
      But I'm sure I have defined the variable, as in the top of my script has 
      Global $vNumber = 0  
      How would I go about fixing this?
    • By D2thunder
      Hi all, I wasnt active in this Forum forum for Years. 😅
      Since I need something with Json in AutoIt i write a little UDF to save a variable to file to read it with another language - and back. After a while until my other script grows i thought - lets integrate all variables wich is possible, because i want have arrays to.
      This is my first public UDF in Autoit - normaly my AutoIt scripts are quick an dirty.... - like the examples for this udf. - someone want to write nice examples?
      I tried to write this UDF properly, cared for processing speed, low mem consumption as possible, a short code, and a high usability without beeing very familiar with arrays.
      What is it for? - convert nearly all types of variables to a json string for saving it in a file or to an ini. Read it back in to a same formated variable and preserv variable types. All array types are supported with nested arrays, the only restriction is ram and array/stringsize - and time if the array is very big. You can use Filters for variable types and some other options.
      If anyone found a bug or any suggestions, please post it.
      get Json.au3 from here Json.au3
      JsonVar 2019.01.25.1.zip
    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
    • By ThePoro
      I have this code 
      It gets variable input from GUI to run a loop and write to a text file then copy it to the clipboard
      But the thing is code runs normally with $px and $py <100 or both > 100 or with $px <10 and $py>100 but It's wrong with 10<$px <100 and $py >100
      Can anyone tell me why?
      Func clone() Local $px = GUICtrlRead($input1) Local $py = GUICtrlRead($input2) Local $temp = "C:\temp.csv" FileDelete($temp) While $px <= $py Local $name = "P"&$px FileWrite($temp, " & $name & ""& @CRLF) $px+= 1 WEnd $a=FileRead($temp) ClipPut($a) MsgBox($MB_ICONINFORMATION,"","Copied to Clipboard") EndFunc  
    • By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

  • Create New...