Sign in to follow this  
Followers 0
deo

Speed: Variable Switch

4 posts in this topic

In an attempt to increase speed of a script that I've wrote, im trying to determine the fastest way of variable checking and thought that it might make for good discussion.

So what is faster? Loading variables from an ini file? Performing IF/ElseIf/Else, Switch, Array iteration? What's your opinion?

~Deo

Share this post


Link to post
Share on other sites



Good to see that people actually care about the speed of their scripts....

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Good to see that people actually care about the speed of their scripts....

Maybe you would harvest more interest if you would have done some testing yourself and shared your findings.

Just to show some good intention, I wrote this here test code. I skipped the reading from file since that is ofcourse slow.

It does three kinds of variable checking, every kind does 500,000 loops, per loop 3 checks. Should be somewhere around 4,500,000 checks. Total runtime is always around 17 seconds on my system. The first two methods are about as fast (little over 5 seconds), the third is a bit slower (little over 6 seconds). Seems there isn't much of a difference to worry about.

On top of that, I consider 4,500,000 checks a ridiculous amount of checks to have to go through for normal scripting processes. If my code would have to check variables that often, I would either try to find more efficient processes, or write something in C or so :whistle:

$a=1

$t = TimerInit()
For $loop = 1 To 500000
    If $a = 1 Then
        $b=1
    ElseIf $a = 2 Then
        $b=2
    ElseIf $a = 3 Then
        $b=3
    EndIf
Next
$tdiff1 = TimerDiff($t)

$t = TimerInit()
For $loop = 1 To 500000
    Select
        Case $a = 1
            $b=1
        Case $a = 2
            $b=2
        Case $a = 3
            $b=3
    EndSelect
Next
$tdiff2 = TimerDiff($t)

$t = TimerInit()
For $loop = 1 To 500000
    Switch $a
        Case 1
            $b=1
        Case 2
            $b=2
        Case 3
            $b=3
    EndSwitch
Next
$tdiff3 = TimerDiff($t)

MsgBox(0,"","500000x 3x If: "&$tdiff1&@CRLF&"500000x 3x Case (in select): "&$tdiff2&@CRLF&"500000x 3x Case (in switch): "&$tdiff3)
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Great Example! I feel kind of dumb that I didnt think of that :whistle:

It does seem, from my test anyway, that performing a switch is fastest.

Thanks!

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