Jump to content

Switch vs. Select vs. If-Else


livewire
 Share

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

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

+=

Addition assignment. e.g. $var += 1 (adds 1 to $var)

-=

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

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