Sign in to follow this  
Followers 0
ddeerr

speed of execution

9 posts in this topic

#1 ·  Posted (edited)

Hi,

I would like a little precision about time execution if is it possible, cause i would like to optimize my code and (try) take good habits when i proramm.

I have a simple example, what is the fastest between :

For $tempa = 1 To UBound($histoArray) - 1 Step 1
            $siClef = StringSplit($histoArray[$tempa], "?")
            If Not @error Then
                GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_4)
                $a = 0
            Else
                $a = 1
                GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_4)
            EndIf
            Select
                Case $statusArray[$tempa] = "V"
                    If $a = 0 Then
                        GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_1)
                    Else
                        GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_1)
                Case $statusArray[$tempa] = "R"
                    If $a = 0 Then
                        GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_2)
                    Else
                        GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_2)
                Case $statusArray[$tempa] = "X"
                    If $a = 0 Then
                        GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_3)
                    Else
                        GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_3)
            EndSelect
        Next
oÝ÷ ٩ݶ­jëh×6
        For $tempa = 1 To UBound($histoArray) - 1 Step 1
            $siClef = StringSplit($histoArray[$tempa], "?")
            If Not @error Then
                GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_4)
                If $statusArray[$tempa] = "V" Then GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_1)
                If $statusArray[$tempa] = "R" Then GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_2)
                If $statusArray[$tempa] = "X" Then GUICtrlCreateListViewItem("|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa], $Treeview_3)
            Else
                GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_4)
                If $statusArray[$tempa] = "V" Then GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_1)
                If $statusArray[$tempa] = "R" Then GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_2)
                If $statusArray[$tempa] = "X" Then GUICtrlCreateListViewItem($histoArray[$tempa] & "| | | | |" & $statusArray[$tempa], $Treeview_3)
            EndIf
        Next
oÝ÷ Ù©Ýjëh×6
Switch
      Case $a = 1
      Case $a = 2
      Case $a = 3
EndSwitch

tx for your replys

Edited by ddeerr

MsgBox(0, "Steven Seagal", "Les poches bourrées de Pesos")

Share this post


Link to post
Share on other sites



Add a timer to the script of interest and see if it matters for your code.

$begin = TimerInit()
Sleep(1000)
$delta = Int(TimerDiff($begin))
MsgBox(0, "Time Difference", $delta)


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

There is a whole thread related to this topic that I found one time by searching. If I remember correctly, the order of speed was If EndIf was fastest, followed by Select EndSelect, followed by For Until, followed by While or Do loops.

late,

Sul

Share this post


Link to post
Share on other sites

Hi,

I would like a little precision about time execution if is it possible, cause i would like to optimize my code and (try) take good habits when i proramm.

I have a simple example, what is the fastest between :

several improvements

- take in a variable the upper value of the loop.

If the compiler is not good it will call the function for each loop

-for code readability put outside the if the parts of code which are identical

-put the listname in a varible as it's used in several lines of the code. gain in redability, not perf

- use a switch and not if as for the first match the program will jump without testing further value. This is ok as your test is mutually exclusive

- place the tests in the order of frquency from high to low, so the test will be ok in the first lines without going through all the code for "V" "R" "X" if X is the most frequently present put it first and so on

$dimHisto=UBound($histoArray) - 1
        For $tempa = 1 To $dimHisto Step 1
            $siClef = StringSplit($histoArray[$tempa], "?")
            If Not @error Then
                $listViewName="|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa]
            Else
                $listViewName=$histoArray[$tempa] & "| | | | |" & $statusArray[$tempa]
            EndIf
        
            GUICtrlCreateListViewItem($listViewName, $Treeview_4)
            switch $statusArray[$tempa]
                Case "V"
                    GUICtrlCreateListViewItem($listViewName, $Treeview_1)
                Case "R"
                    GUICtrlCreateListViewItem($listViewName, $Treeview_2)
                Case "X"
                    GUICtrlCreateListViewItem($listViewName, $Treeview_3)
            EndSwitch
        Next

Share this post


Link to post
Share on other sites

several improvements

- take in a variable the upper value of the loop.

If the compiler is not good it will call the function for each loop

-for code readability put outside the if the parts of code which are identical

-put the listname in a varible as it's used in several lines of the code. gain in redability, not perf

- use a switch and not if as for the first match the program will jump without testing further value. This is ok as your test is mutually exclusive

- place the tests in the order of frquency from high to low, so the test will be ok in the first lines without going through all the code for "V" "R" "X" if X is the most frequently present put it first and so on

$dimHisto=UBound($histoArray) - 1
        For $tempa = 1 To $dimHisto Step 1
            $siClef = StringSplit($histoArray[$tempa], "?")
            If Not @error Then
                $listViewName="|" & $siClef[1] & "|" & $siClef[2] & "|" & $siClef[4] & "|" & $siClef[3] & "|" & $statusArray[$tempa]
            Else
                $listViewName=$histoArray[$tempa] & "| | | | |" & $statusArray[$tempa]
            EndIf
        
            GUICtrlCreateListViewItem($listViewName, $Treeview_4)
            switch $statusArray[$tempa]
                Case "V"
                    GUICtrlCreateListViewItem($listViewName, $Treeview_1)
                Case "R"
                    GUICtrlCreateListViewItem($listViewName, $Treeview_2)
                Case "X"
                    GUICtrlCreateListViewItem($listViewName, $Treeview_3)
            EndSwitch
        Next
Hum, this is cooll i will do that try to clear and simplify the code.

Finally If is greatest loop for simple thing and switch is beter than select i suppose (for redabilty).

There is a whole thread related to this topic that I found one time by searching. If I remember correctly, the order of speed was If EndIf was fastest, followed by Select EndSelect, followed by For Until, followed by While or Do loops.

late,

Sul

Could you give this link if not i will search later.

Tx all


MsgBox(0, "Steven Seagal", "Les poches bourrées de Pesos")

Share this post


Link to post
Share on other sites

Sorry, I have no idea what I used to search for, nor did I bookmark it. I think I was looking up some combination of 'loop' 'speed' 'performance' and maybe 'array'. As well, I may have been also using 'While loop speed' or 'Do loop speed'.

later,

Sul

Share this post


Link to post
Share on other sites

Sorry, I have no idea what I used to search for, nor did I bookmark it. I think I was looking up some combination of 'loop' 'speed' 'performance' and maybe 'array'. As well, I may have been also using 'While loop speed' or 'Do loop speed'.

later,

Sul

Thanks for that, is there a kind person which know and can indicate me where can i find information concerning memory usage of autoIt for example when you put a Local Variable in a function if when program quit function the memory is free.

otherwise tx for all :lmao:


MsgBox(0, "Steven Seagal", "Les poches bourrées de Pesos")

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Also, use += or *= or -= etc. whenever you can over their longer counterparts. Below is a script that tests the speed of a function that uses the += operator and one that doesnt.

#include <Array.au3>
$aa = _ArrayCreate(-1)
$bb = _ArrayCreate(-1)

$sum = 0
$avg = 0

func a()
    $timer = TimerInit()
    $y=0
    for $x = 0 to 10000
        $y += 1
    Next
    $diff = TimerDiff($timer)
    return $diff
EndFunc

func b()
    $timer = TimerInit()
    $y=0
    for $x = 0 to 10000
        $y = $y + 1
    Next
    $diff = TimerDiff($timer)
    return $diff
EndFunc


for $i = 0 to 50
    $d = a()
    _ArrayAdd($aa, $d)
Next

    for $v = 1 to Ubound($aa) -1
        $sum += $aa[$v]
    Next
    $avg = $sum / (Ubound($aa) -1)
    ConsoleWrite("a_avg="&$avg&@cr)

for $i = 0 to 50
    $d = b()
    _ArrayAdd($bb, $d)
Next

    for $v = 1 to Ubound($bb) -1
        $sum += $bb[$v]
    Next
    $avg = $sum / (Ubound($bb) -1)
    ConsoleWrite("b_avg="&$avg&@cr)

After 50 iterations of the method, which in turn interates 10000 times, the += operator appears to be more than twice as fast as the 'old' method.

I know it doesnt apply in this case, but its good to keep in the back of your mind for future scripts that require a fast runtime.

Edited by ame1011

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]

Share this post


Link to post
Share on other sites

After 50 iterations of the method, which in turn interates 10000 times, the += operator appears to be more than twice as fast as the 'old' method.

I know it doesnt apply in this case, but its good to keep in the back of your mind for future scripts that require a fast runtime.

Of course i'll remember that thanks a lot :lmao:


MsgBox(0, "Steven Seagal", "Les poches bourrées de Pesos")

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