TheDcoder Posted March 20, 2015 Share Posted March 20, 2015 Hello, I am experimenting with "If" & "Switch" to determine which method is faster... I am currently using this script to test them: expandcollapse popupGlobal $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... Thanks in Advance EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
BrewManNH Posted March 20, 2015 Share Posted March 20, 2015 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 GudeHow 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 Link to comment Share on other sites More sharing options...
TheDcoder Posted March 20, 2015 Author Share Posted March 20, 2015 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? EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
water Posted March 20, 2015 Share Posted March 20, 2015 Sometimes "Switch" wins, Sometimes "If" wins... Looks like they are equal in speed. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Solution TheDcoder Posted March 20, 2015 Author Solution Share Posted March 20, 2015 (edited) Sorry guys, such a simple question, I have determined the winner You can run this code (multiple times) to find it out yourself: expandcollapse popupGlobal $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 March 20, 2015 by TheDcoder EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
jdelaney Posted March 20, 2015 Share Posted March 20, 2015 (edited) 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 March 20, 2015 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. Link to comment Share on other sites More sharing options...
JohnOne Posted March 20, 2015 Share Posted March 20, 2015 Switch might be quicker, but that is not a fair test. The difference is much less. expandcollapse popupLocal $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) AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jdelaney Posted March 20, 2015 Share Posted March 20, 2015 (edited) 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: expandcollapse popupSleep(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 March 20, 2015 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. Link to comment Share on other sites More sharing options...
Bowmore Posted March 20, 2015 Share Posted March 20, 2015 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. TheDcoder and jdelaney 2 "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 Link to comment Share on other sites More sharing options...
TheDcoder Posted March 20, 2015 Author Share Posted March 20, 2015 (edited) @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 March 20, 2015 by TheDcoder EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
JohnOne Posted March 20, 2015 Share Posted March 20, 2015 Also, Switch has the home team advantage that if it;s a dead heat, Switch takes credit TheDcoder 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Bowmore Posted March 20, 2015 Share Posted March 20, 2015 (edited) @TheDcoderYour 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.For your information$true = $i = $i ? True : Falseis faster than either of the If Then versions Edited March 20, 2015 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 Link to comment Share on other sites More sharing options...
TheDcoder Posted March 20, 2015 Author Share Posted March 20, 2015 Ideally for a test like this TimerInit() and TimerDiffer should be outside the loops otherwise you are measuring the speed of these functions I don't understand, code please? $true = $i = $i ? True : False Testing purposes... TD EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
Bowmore Posted March 20, 2015 Share Posted March 20, 2015 I don't understand, co expandcollapse popup#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 de please? "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 Link to comment Share on other sites More sharing options...
jdelaney Posted March 20, 2015 Share Posted March 20, 2015 (edited) @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 March 20, 2015 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. Link to comment Share on other sites More sharing options...
TheDcoder Posted March 20, 2015 Author Share Posted March 20, 2015 Because your script was flawed Sorry, I can't understand as I am a newbie.... EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
water Posted March 20, 2015 Share Posted March 20, 2015 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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now