Sign in to follow this  
Followers 0

Concerning If Then/If Then Else...

3 posts in this topic

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



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.

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.