Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Concerning If Then/If Then Else...


  • Please log in to reply
2 replies to this topic

#1 money

money

    Seeker

  • Active Members
  • 23 posts

Posted 10 October 2011 - 06:25 PM

A reply to this topic regarding "If Then vs. If Then Endif": which belongs on a entirely topic:

http://www.autoitscript.com/forum/topic/...2-may-11/page__view__findpost_

Results: Variable [pun intended]

AutoIt         
Global $eatit, $choice, $stop_watch, $cheese = 1, $laps = 10000 Global $winner[2] = [100000000, 0], $rat[4] = [0,0,0,0], $names[4] = ["If then","If Then EndIf","Switch","Select"] ConsoleWrite("The mouse are asleep, releasing the cheese " &@LF) ConsoleWrite("The cheese is being tested: ") $eatit = TimerInit() ;WARMUP For $i = 1 To 10     Sleep(10)     If $cheese = $cheese Then $cheese = $cheese     If $cheese = $cheese Then         $cheese = $cheese     EndIf     Switch $cheese         Case $cheese             $cheese = $cheese     EndSwitch     Select         Case $cheese = $cheese             $cheese = $cheese     EndSelect Next ConsoleWrite("FDA Approved" &@LF) $choice = InputBox("Pick the winning rat", StringFormat("0 = If then\r\n1 = If Then EndIf\r\n2 = Switch\r\n3 = Select"), 0) ConsoleWrite(@lf& "Setting race: 4 Contestants after " & $cheese & " cheese for "& $laps & " laps" &@lf) For $i = 1 To $laps     ;=============================     ; if then     $stop_watch = TimerInit()     If $cheese = 1 Then $cheese = 1     $rat[0] += TimerDiff($stop_watch)     ;=============================     ; if then endif     $stop_watch = TimerInit()     If $cheese = 1 Then         $cheese = 1     EndIf     $rat[1] += TimerDiff($stop_watch)     ;=============================     ; switch     $stop_watch = TimerInit()     Switch $cheese         Case 1             $cheese = 1     EndSwitch     $rat[2] += TimerDiff($stop_watch)     ;=============================     ; select     $stop_watch = TimerInit()     Select         Case $cheese = 1             $cheese = 1     EndSelect     $rat[3] += TimerDiff($stop_watch) Next $eatit = TimerDiff($eatit) ConsoleWrite(@lf& "+ the results are in!!!" &@lf) ConsoleWrite( "> ========================================================================" &@lf) ConsoleWrite( "> "& $names[0] &": "&@TAB&  "avg: "& Round($rat[0] / $laps, 15) &", total: "& Round($rat[0],15) &@lf) ConsoleWrite( "> "& $names[1] &": "&@TAB&  "avg: "& Round($rat[1] / $laps, 15) &", total: "& Round($rat[1],15) &@lf) ConsoleWrite( "> "& $names[2] &": "&@TAB&  "avg: "& Round($rat[2] / $laps, 15) &", total: "& Round($rat[2],15) &@lf) ConsoleWrite( "> "& $names[3] &": "&@TAB&  "avg: "& Round($rat[3] / $laps, 15) &", total: "& Round($rat[3],15) &@lf) ConsoleWrite( "> ========================================================================" &@lf) ConsoleWrite( "+ ...and the winner was: " ) For $i = 0 To UBound($rat)-1     If $rat[$i] < $winner[0] Then         $winner[0] = $rat[$i]         $winner[1] = $i     EndIf Next ConsoleWrite( StringUpper( $names[$winner[1]]) & @lf) If $winner[1] = $choice Then     ConsoleWrite("+Good choice, you picked the right rat!" & @lf) Else     ConsoleWrite("Better luck next time." & @lf) EndIf ConsoleWrite("> Notice: A total of " & (Round($eatit,4)) &" ms could been taken to EAT the cheese!" &@lf)








#2 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 10,043 posts

Posted 10 October 2011 - 07:11 PM

Interesting results, but skewed.

If you add the line $cheese = Not $cheese just after the line For $i = 1 To $laps you will probably see very different results. By toggling between 1 and 0 (True and False), the results jump all over the 4 different choices and the differences are so small as to be negligible.

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 an XP machine, 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!

 

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.
GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips. Latin Square password generator

 

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.


#3 Ascend4nt

Ascend4nt

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,333 posts

Posted 11 October 2011 - 01:53 AM

BrewMan is correct here. True/False conditions cause the results to be different.

For example, if $a=1 then
If $a Then $a=1

is slower than
If $a Then $a=1 EndIf


yet, where $a=0,
If $a Then $a=0

is faster than
If $a Then $a=0 EndIf

Edited by Ascend4nt, 11 October 2011 - 01:53 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users