Sign in to follow this  
Followers 0

Concerning If Then/If Then Else...

3 posts in this topic

#1 ·  Posted

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

Results: Variable [pun intended]

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)

Share this post


Link to post
Share on other sites



#2 ·  Posted

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 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 this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0