# Speed: Variable Switch

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

##### Share on other sites

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

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

```\$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)```

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

##### Share on other sites

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

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

Thanks!

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...