Sign in to follow this  
Followers 0
trancexx

True logic

5 posts in this topic

#1 ·  Posted (edited)

Logic statements are very basics of any programing language. Implication of such is a way to bring sense.

$x = 5
$y = 0

If $x = 5 Then $y +=1

Nothing new there. It says, if x equals 5 then increase y by amount of 1.

What I'm interested in is the speed that AutoIt's engine process that implications. Usually this kind of implications are done inside some kind of loop and you will need to get out of that loop as soon as possible for your script to have somewhere "better" effect.

Is "If..Then" fast or slow? The answer depends on what is compared to.

If you write that implication using every available method in AutoIt you will se that "If...Then" (one liner) is the slowest method.

Possible methods:

$x = 5
$y = 0
If $x = 5 Then $y +=1

$x = 5
$y = 0
If $x = 5 Then
    $y +=1
EndIf

$x = 5
$y = 0
Select 
    Case $x = 5
        $y +=1
EndSelect

$x = 5
$y = 0
Switch $x
    Case 5
        $y +=1
EndSwitch

Now for why I'm writing this.

There is one method more to make that implication. That is by using logic True in it's primary meaning (True = 1, False = 0).

$x = 5
$y = 0
$y += ($x = 5) And 1

No If...Then, Select..EndSelect or Switch...EndSwitch there but still all cases covered in just one line.

The beauty of that method is that it's the fastest (almost by half compared to If...Then).

This method is implemented in new _Atan2() function that I'm suggesting (New math functions thread) and proofs its self by gaining aditional speed over current function (that has bug anyway).

This is another example of it.

Little script to compare speed:

$r = 0
$b = 0
$c = 0
$rounds = 100000
ConsoleWrite("Result:                Speed:" & @CRLF)

For $w = 1 To 5
    
 ;xxxxxxxxxxxxxxxxxxxxxxxxxxx If...Then xxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
    $start = TimerInit()
    For $i = 1 To $rounds
        If Mod($i, 7) <> 0  Then  $r += 1
    Next
    $end = TimerDiff($start)

    If $w > 1 Then ConsoleWrite($r & "                  " & 1000 * ($rounds - 1) / $end & " per sec using If...Then" & @CRLF)
    $start = 0
    $end = 0
    $r = 0

 ;xxxxxxxxxxxxxxxxxxxxxx True - Not True statement xxxxxxxxxxxxxxxxx

    $start1 = TimerInit()
    For $j = 1 To $rounds
        $b += Mod($j, 7) And 1
    Next
    $end1 = TimerDiff($start1)

    If $w > 1 Then ConsoleWrite($b & "                  " & 1000 * ($rounds - 1) / $end1 & " per sec using logic True" & @CRLF)
    $start1 = 0
    $end1 = 0
    $b = 0

 ;xxxxxxxxxxxxxxxxxxxxxxx Switch...EndSwitch xxxxxxxxxxxxxxxxxxxxxxx 

    $start2 = TimerInit()
    For $i = 1 To $rounds
        Switch Mod($i, 7)
            Case 0
            Case Else   
                $c += 1
        EndSwitch       
    Next
    $end2 = TimerDiff($start2)

    If $w > 1 Then ConsoleWrite($c & "                  " & 1000 * ($rounds - 1) / $end2 & " per sec using Switch...EndSwitch" & @CRLF)
    $start2 = 0
    $end2 = 0
    $c = 0

 ;xxxxxxxxxxxxxxxxxxxxxxxxxx End xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

ConsoleWrite(@CRLF)

Next
Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites



Very well said trancexx, and useful too. I didn't know that If/Then's were that slowed compared to Switch/Endswitch and True/False statements! This is very useful, thanks.

Share this post


Link to post
Share on other sites

Further simplifying first post example would lead to:

$x = 5
$y = 0

$y += $x = 5

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Further simplifying first post example would lead to:

$x = 5
$y = 0

$y += $x = 5
Note that this simplification will only work when adding 1, because the $x = 5 condition can only return 1 or 0. Nice work, though, still needs to be tested with adding other values to see how far this can be pushed.

Share this post


Link to post
Share on other sites

You can always multiply 1 with some other number to add that number to $y


♡♡♡

.

eMyvnE

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