Jump to content

Speed: Variable Switch


deo
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...