# Which is the fastest?

## Recommended Posts

Hello, I am experimenting with "If" & "Switch" to determine which method is faster...

I am currently using this script to test them:

```Global \$number = Random(0 , 10, 1)
Global \$pass = False

Global \$time_if = TimerInit()
For \$i = 0 To \$number
If \$i = \$number Then \$pass = True
Next

\$time_if = TimerDiff(\$time_if)
MsgBox(64, "Timer", '"If" took ' & \$time_if & ' milli-seconds.')
\$pass = False

Global \$time_switch = TimerInit()

Switch \$number
Case 0
\$pass = True

Case 1
\$pass = True

Case 2
\$pass = True

Case 3
\$pass = True

Case 4
\$pass = True

Case 5
\$pass = True

Case 6
\$pass = True

Case 7
\$pass = True

Case 8
\$pass = True

Case 9
\$pass = True

Case 10
\$pass = True

EndSwitch

\$time_switch = TimerDiff(\$time_switch)
MsgBox(64, "Timer", '"Switch" took ' & \$time_if & ' milli-seconds.')

MsgBox(64, "The winner is...", "The winner is...", 3)

If \$time_if < \$time_switch Then
MsgBox(64, "The winner is... If!!!", "The winner is... If!!!")
Global \$diff = \$time_switch - \$time_if
Else
MsgBox(64, "The winner is... Switch!!!", "The winner is... Switch!!!")
Global \$diff = \$time_if - \$time_switch
EndIf

MsgBox(64, "Intel", "The difference was only " & \$diff & " milliseconds.")```

Sometimes "Switch" wins, Sometimes "If" wins...

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

##### Share on other sites

What is your question? It appears you've already done the testing, so you tell us.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

What is your question? It appears you've already done the testing, so you tell us.

Ummmm..... I mean "I can't crown a winner", not that I have tested and determined the winner

Understand?

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

##### Share on other sites

Sometimes "Switch" wins, Sometimes "If" wins...

Looks like they are equal in speed.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Taks Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:

##### Share on other sites

Sorry guys, such a simple question, I have determined the winner

You can run this code (multiple times) to find it out yourself:

```Global \$number = Random(0 , 10, 1)
Global \$pass = False

Global \$time_if = TimerInit()
For \$i = 0 To \$number
If \$i = \$number Then \$pass = True
Next

\$time_if = TimerDiff(\$time_if)
MsgBox(64, "Timer", '"If" took ' & \$time_if & ' milli-seconds.')
\$pass = False

Global \$time_switch = TimerInit()

Switch \$number
Case 0
\$pass = True

Case 1
\$pass = True

Case 2
\$pass = True

Case 3
\$pass = True

Case 4
\$pass = True

Case 5
\$pass = True

Case 6
\$pass = True

Case 7
\$pass = True

Case 8
\$pass = True

Case 9
\$pass = True

Case 10
\$pass = True

EndSwitch

\$time_switch = TimerDiff(\$time_switch)
MsgBox(64, "Timer", '"Switch" took ' & \$time_if & ' milli-seconds.')

MsgBox(64, "The winner is...", "The winner is...", 3)

If \$time_if < \$time_switch Then
MsgBox(64, "The winner is... If!!!", "The winner is... If!!!")
Global \$diff = \$time_switch - \$time_if
Else
MsgBox(64, "The winner is... Switch!!!", "The winner is... Switch!!!")
Global \$diff = \$time_if - \$time_switch
EndIf

MsgBox(64, "Intel", "The difference was only " & \$diff & " milliseconds." & @CRLF & '(BTW, The random number was ' & \$number & '.)')```

(or You can cheat by hitting "Show")

The winner is..... Switch!!!!, "If" was faster than "Switch" when the random number was 0.

TD

Edited by TheDcoder

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

##### Share on other sites

You can speed up the If statement by making it three lines...

```\$true = True
Sleep(5000)
\$iTime = TimerInit()
For \$i = 0 to 1000000
If \$true Then \$bTrue=True
Next
ConsoleWrite(TimerDiff(\$iTime) & @CRLF)

\$iTime = TimerInit()
For \$i = 0 to 1000000
If \$true Then
\$bTrue=True
EndIf
Next
ConsoleWrite(TimerDiff(\$iTime) & @CRLF)```

output...nearly 50% quicker with 3 lines:

659.734839359863
369.013181058162

Edit: one run will not give you a statistical result...you need at least 20 executions to see the true 'winner'.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

##### Share on other sites

Switch might be quicker, but that is not a fair test.

The difference is much less.

```Local \$number = Random(0, 10, 1)

Local \$pass = False

Local \$time_if = TimerInit()

If \$number = 0 Then
\$pass = True
ElseIf \$number = 1 Then
\$pass = True
ElseIf \$number = 2 Then
\$pass = True
ElseIf \$number = 3 Then
\$pass = True
ElseIf \$number = 4 Then
\$pass = True
ElseIf \$number = 5 Then
\$pass = True
ElseIf \$number = 6 Then
\$pass = True
ElseIf \$number = 7 Then
\$pass = True
ElseIf \$number = 8 Then
\$pass = True
ElseIf \$number = 9 Then
\$pass = True
ElseIf \$number = 10 Then
\$pass = True
EndIf

\$time_if = TimerDiff(\$time_if)
ConsoleWrite("If took " & \$time_if & ' milli-seconds.' & @LF)
\$pass = False

Local \$time_switch = TimerInit()

Switch \$number
Case 0
\$pass = True
Case 1
\$pass = True
Case 2
\$pass = True
Case 3
\$pass = True
Case 4
\$pass = True
Case 5
\$pass = True
Case 6
\$pass = True
Case 7
\$pass = True
Case 8
\$pass = True
Case 9
\$pass = True
Case 10
\$pass = True
EndSwitch

\$time_switch = TimerDiff(\$time_switch)

ConsoleWrite("Switch took " & \$time_if & ' milli-seconds.' & @LF)
ConsoleWrite("The winner is..." & @LF)
If \$time_if < \$time_switch Then
ConsoleWrite("The winner is... If!!!" & @LF)
Local \$diff = \$time_switch - \$time_if
Else
ConsoleWrite("The winner is... Switch!!!" & @LF)
Local \$diff = \$time_if - \$time_switch
EndIf
ConsoleWrite("The difference was only " & \$diff & " milliseconds." & @CRLF & '(BTW, The random number was ' & \$number & '.)' & @LF)```

Monkey's are, like, natures humans.

##### Share on other sites

Agreed with JohnOne...I was going down the same lines...the increase in times, moving down the switch statement is less than moving down the if statements...The if statements degrade in time more quickly than the switches:

```Sleep(5000)
For \$number = 1 To 9
\$pass = False
\$iTime = TimerInit()
For \$i = 0 To 100000
If 0 = \$number  Then
\$pass = True
ElseIf 1 = \$number Then
\$pass = True
ElseIf 2 = \$number Then
\$pass = True
ElseIf 3 = \$number Then
\$pass = True
ElseIf 4 = \$number Then
\$pass = True
ElseIf 5 = \$number Then
\$pass = True
ElseIf 6 = \$number Then
\$pass = True
ElseIf 7 = \$number Then
\$pass = True
ElseIf 8 = \$number Then
\$pass = True
ElseIf 9 = \$number Then
\$pass = True
EndIf
Next
ConsoleWrite(\$number & " if state " & TimerDiff(\$iTime) & @CRLF)

\$iTime = TimerInit()
For \$i = 0 To 100000
Switch \$number
Case 0
\$pass = True
Case 1
\$pass = True
Case 2
\$pass = True
Case 3
\$pass = True
Case 4
\$pass = True
Case 5
\$pass = True
Case 6
\$pass = True
Case 7
\$pass = True
Case 8
\$pass = True
Case 9
\$pass = True
Case 10
\$pass = True
EndSwitch
Next
ConsoleWrite(\$number & " switch   " & TimerDiff(\$iTime) & @CRLF)
Next```

output:

1 if state 126.848619531115 (anomaly...probably not a long enough sleep to allow the startup processes to level out)
1 switch   58.6771617177711
2 if state 96.1688407026287
2 switch   67.1120696413732
3 if state 117.517679039024
3 switch   72.7790551318035
4 if state 139.159588161423
4 switch   79.5062840591444
5 if state 158.816142270472
5 switch   87.0183611128137
6 if state 181.916563203366
6 switch   92.7378028528788
7 if state 201.045989021249
7 switch   99.1226407160278
8 if state 221.029539425576
8 switch   105.378048484969
9 if state 241.681336836118
9 switch   112.126944124116

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

##### Share on other sites

It depends on the values and types of data you are checking. I would use whichever if or switch represent the logic of your code in a manner that is easiest to understand. The differences between the 2 methods are so small that it makes no difference in normal circumstances. The only time it would make any noticeable difference is inside a loop that is being executed millions of times when detailed fine tuning can make a difference. Meanwhile just use the method that is easiest four you to understand.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

##### Share on other sites

@jdelaney The winner was is still 1 liner (ran 100 times)....

Code:

```Global \$true = False
Global \$total_time_1_liner = 0
Global \$total_time_3_liner = 0

For \$i = 0 To 100
\$diff = TimerInit()
For \$i = 0 to 100 * 100 *100
If \$i = \$i Then \$true = True
Next
\$total_time_1_liner += TimerDiff(\$diff)
Next

\$true = False

For \$i = 0 To 100
For \$i = 0 to 100 * 100 *100
\$diff = TimerInit()
If \$i = \$i Then
\$true = True
EndIf
\$total_time_3_liner += TimerDiff(\$diff)
Next
Next

If \$total_time_1_liner < \$total_time_3_liner Then
MsgBox(64, "Intel", "The winner is... 1 liner!!!, difference was only " & \$total_time_3_liner - \$total_time_1_liner & " milli-seconds.")
Else
MsgBox(64, "Intel", "The winner is... 3 liner!!!, difference was only " & \$total_time_1_liner - \$total_time_3_liner & " milli-seconds.")
EndIf```

@JohnOne Thanks

TD

Edited by TheDcoder

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

##### Share on other sites

Also, Switch has the home team advantage that if it;s a dead heat, Switch takes credit

Monkey's are, like, natures humans.

##### Share on other sites

@TheDcoder

Your test in #10 gives the wrong result because you have TimerInit() and TimerDiff() in different positions with the loops it you put them in the same relative positions you will find that the 3 line version of if is faster. Ideally for a test like this TimerInit() and TimerDiffer should be outside the loops otherwise you are measuring the speed of these functions.

\$true = \$i = \$i ? True : False

is faster than either of the If Then versions

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

##### Share on other sites

Ideally for a test like this TimerInit() and TimerDiffer should be outside the loops otherwise you are measuring the speed of these functions

\$true = \$i = \$i ? True : False

Testing purposes...

TD

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

##### Share on other sites

I don't understand, co

```#include <math.au3>
Global \$true = False
Global \$total_time_1_liner = 0
Global \$total_time_3_liner = 0
Global \$total_time_0_liner = 0

\$true = False
\$diff = TimerInit()
For \$i = 0 To 100
For \$j = 0 to 100 * 100
If \$j = \$j Then \$true = True
Next
Next
\$total_time_1_liner = TimerDiff(\$diff)

\$true = False
\$diff = TimerInit()
For \$i = 0 To 100
For \$j = 0 to 100 * 100
If \$j = \$j Then
\$true = True
EndIf
Next
Next
\$total_time_3_liner = TimerDiff(\$diff)

\$true = False
\$diff = TimerInit()
For \$i = 0 To 100
For \$j = 0 to 100 * 100
\$true = \$j = \$j ? True : False
Next
Next
\$total_time_0_liner = TimerDiff(\$diff)

If \$total_time_1_liner < \$total_time_3_liner  And \$total_time_1_liner < \$total_time_0_liner Then
MsgBox(64, "Intel", "The winner is... 1 liner!!!, difference was only " & _Max(\$total_time_3_liner - \$total_time_1_liner,\$total_time_0_liner - \$total_time_1_liner)  & " milli-seconds.")
ElseIf \$total_time_3_liner < \$total_time_0_liner Then
MsgBox(64, "Intel", "The winner is... 3 liner!!!, difference was only " & _Max(\$total_time_1_liner - \$total_time_3_liner,\$total_time_0_liner - \$total_time_3_liner) & " milli-seconds.")
Else
MsgBox(64, "Intel", "The winner is... 0 liner!!!, difference was only " & _Max(\$total_time_3_liner - \$total_time_0_liner,\$total_time_1_liner - \$total_time_0_liner) & " milli-seconds.")
EndIf

\$total_time_1_liner = 0
\$total_time_3_liner = 0
\$total_time_0_liner = 0

\$true = False
\$diff = TimerInit()
For \$i = 0 To 100
For \$j = 0 to 100 * 100
If \$j = \$i Then \$true = True
Next
Next
\$total_time_1_liner = TimerDiff(\$diff)

\$true = False
\$diff = TimerInit()
For \$i = 0 To 100
For \$j = 0 to 100 * 100
If \$j = \$i Then
\$true = True
EndIf
Next
Next
\$total_time_3_liner = TimerDiff(\$diff)

\$true = False
\$diff = TimerInit()
For \$i = 0 To 100
For \$j = 0 to 100 * 100
\$true = \$j = \$i ? True : False
Next
Next
\$total_time_0_liner = TimerDiff(\$diff)

If \$total_time_1_liner < \$total_time_3_liner  And \$total_time_1_liner < \$total_time_0_liner Then
MsgBox(64, "Intel", "The winner is... 1 liner!!!, difference was only " & _Max(\$total_time_3_liner - \$total_time_1_liner,\$total_time_0_liner - \$total_time_1_liner)  & " milli-seconds.")
ElseIf \$total_time_3_liner < \$total_time_0_liner Then
MsgBox(64, "Intel", "The winner is... 3 liner!!!, difference was only " & _Max(\$total_time_1_liner - \$total_time_3_liner,\$total_time_0_liner - \$total_time_3_liner) & " milli-seconds.")
Else
MsgBox(64, "Intel", "The winner is... 0 liner!!!, difference was only " & _Max(\$total_time_3_liner - \$total_time_0_liner,\$total_time_1_liner - \$total_time_0_liner) & " milli-seconds.")
EndIf```

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

##### Share on other sites

@jdelaney The winner was is still 1 liner (ran 100 times)....

Because your script was flawed.  I assure you, it is not.  I've already demonstrated, above.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

##### Share on other sites

Sorry, I can't understand as I am a newbie....

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

##### Share on other sites

He already described in post #12 what is wrong with your code (= the way you measure execution time).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Taks Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

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