# Switch vs. Select vs. If-Else

## Recommended Posts

I don't know if most of you know this, but newbees may benefit from this. Switch performs faster than If-ElseIf-Else and Select.

-Livewire

```\$repetitions = 1000000
\$x = 10
\$y = \$repetitions

\$timestamp = TimerInit()
While \$y > 0
If \$x = 0 Then
; Do nothing
ElseIf \$x = 1 Then
; Do nothing
ElseIf \$x = 2 Then
; Do nothing
ElseIf \$x = 3 Then
; Do nothing
ElseIf \$x = 4 Then
; Do nothing
ElseIf \$x = 5 Then
; Do nothing
ElseIf \$x = 6 Then
; Do nothing
ElseIf \$x = 7 Then
; Do nothing
ElseIf \$x = 8 Then
; Do nothing
ElseIf \$x = 9 Then
; Do nothing
ElseIf \$x = 10 Then
\$y -= 1
Else
; Do nothing
EndIf
WEnd
\$diff = "If-ElseIf-Else - " & TimerDiff(\$timestamp)/1000 & " seconds" & @CRLF
\$y = \$repetitions
\$timestamp = TimerInit()
While \$y > 0
Select
Case \$x = 0
; Do nothing
Case \$x = 1
; Do nothing
Case \$x = 2
; Do nothing
Case \$x = 3
; Do nothing
Case \$x = 4
; Do nothing
Case \$x = 5
; Do nothing
Case \$x = 6
; Do nothing
Case \$x = 7
; Do nothing
Case \$x = 8
; Do nothing
Case \$x = 9
; Do nothing
Case \$x = 10
\$y -= 1
Case Else
; Do nothing
EndSelect
WEnd
\$diff = \$diff & "Select-Case - " & TimerDiff(\$timestamp)/1000 & " seconds" & @CRLF
\$y = \$repetitions
\$timestamp = TimerInit()
While \$y > 0
Switch \$x
Case 0
; Do nothing
Case 1
; Do nothing
Case 2
; Do nothing
Case 3
; Do nothing
Case 4
; Do nothing
Case 5
; Do nothing
Case 6
; Do nothing
Case 7
; Do nothing
Case 8
; Do nothing
Case 9
; Do nothing
Case 10
\$y -= 1
Case Else
; Do nothing
EndSwitch
WEnd
\$diff = \$diff & "Switch-Case - " & TimerDiff(\$timestamp)/1000 & " seconds"
MsgBox(0,"Done",\$diff)```

##### Share on other sites

That's a long execution time. Had to stop it and set it to 100,000 instead, took about a minute (23.5/22.5/14)

Writing AutoIt scripts since
`_DateAdd("d", -2, _NowCalcDate())`

##### Share on other sites

just an FYI, put

ProcessSetPriority(@AutoItPid, 5)

at the top

else your cpu time could get "stolen" in the middle of the test, thus making wrong conclusions. (also its faster )

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

##### Share on other sites

Switch performs faster than If-ElseIf-Else and Select.

With release version of AutoIT I got:

C:\Program Files\AutoIt3\Examples\TestOfSpeedSwitchCaseIFTHEN.au3 (69) : ==> "Case" statement with no matching "Select" statement.:

Case 0

Worked okay with beta 75.

By the way, I take it that "\$y -= 1" means decrement \$y by one, but where do you find that nomenclature? Is there a place that spells out all of these special statements? Thanks...

Edited by jefhal

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

##### Share on other sites

With release version of AutoIT I got:

Worked okay with beta 75.

By the way, I take it that "\$y -= 1" means decrement \$y by one, but where do you find that nomenclature? Is there a place that spells out all of these special statements? Thanks...

Switch = beta only

Helpfile

+=

-=

Subtraction assignment.

*=

Multiplication assignment.

/=

Division assignment.

&=

Concatenation assignment. e.g. \$var = "one", and then \$var &= 10 (\$var now equals "one10")

Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

##### Share on other sites

whats the point of select case AND switch? they do pretty much the same thing

##### Share on other sites

Switch only analyzes the variable once, and allows for single-variable conditional statements.

Writing AutoIt scripts since
`_DateAdd("d", -2, _NowCalcDate())`

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...