Jump to content

Recommended Posts

Posted

I was reading some docs recently for If...Then and noticed one of the paragraphs:

Quote

This version of the If statement is used to execute a single statement without the overhead of an EndIf.

I've added bold to the part that I have a concern about. According to the docs, that tells me that a single line If statement performs better in comparison to and If statement that ends with EndIf.

That made me start to think about converting all of my If...EndIf statements to single line If statements.

However, since I have been performance testing everything that goes into my current project, I realized pretty quickly that the docs were wrong about the EndIf "overhead".

So I decided to search the forums here since these forums are a gold mine of information. I came across a great post (with script) by the great @guinness for comparing the performance. I have updated the script a bit:

Global $iTimer, $iVariable = 1

$iTimer1 = TimerInit()
For $A = 1 To 10000
    If $iVariable Then $iVariable = 1
Next
$iTimer1 = Round(TimerDiff($iTimer1), 1)
ConsoleWrite("10,000 runs (single line If statement): " & @TAB & $iTimer1 & "ms" & @CRLF)

$iTimer2 = TimerInit()
For $A = 1 To 10000
    If $iVariable Then
        $iVariable = 1
    EndIf
Next
$iTimer2 = Round(TimerDiff($iTimer2), 1)
ConsoleWrite("10,000 runs (If statement with EndIf): " & @TAB & @TAB & $iTimer2 & "ms" & @CRLF & @CRLF)

$iDifference = Round(($iTimer1 - $iTimer2) / $iTimer2, 2)
ConsoleWrite("Percentage Difference: " & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & $iDifference * 100 & "%" & @CRLF)

The difference is really quite interesting.

But as I mentioned, the problem that I have is with the wording "without the overhead of an EndIf" in the documentation. If it wasn't for me doing performance testing comparisons recently, I probably would have went ahead and changed dozens of If...EndIf statements in my current project to single line If statements and these get called sometimes dozens of times per second.

As always, thank you for your time. :)

Posted
Global $iTimer, $iVariable = 1

For $tryNo = 1 To 3
    Sleep(1000)
    $iTimer1 = TimerInit()
    For $A = 1 To 10000
        If $iVariable Then $iVariable = 1
    Next
    $iTimer1 = Round(TimerDiff($iTimer1), 1)
    ConsoleWrite("10,000 runs (single line If statement): " & @TAB & $iTimer1 & "ms" & @CRLF)

    $iTimer2 = TimerInit()
    For $A = 1 To 10000
        If $iVariable Then
            $iVariable = 1
        EndIf
    Next
    $iTimer2 = Round(TimerDiff($iTimer2), 1)
    ConsoleWrite("10,000 runs (If statement with EndIf): " & @TAB & @TAB & $iTimer2 & "ms" & @CRLF)

    $iDifference = Round(($iTimer1 - $iTimer2) / $iTimer2, 2)
    ConsoleWrite("Percentage Difference: " & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & $iDifference * 100 & "%" & @CRLF & @CRLF)
Next
Exit
10,000 runs (single line If statement):     40.5ms
10,000 runs (If statement with EndIf):      3.1ms
Percentage Difference:                      1206%

10,000 runs (single line If statement):     5.7ms
10,000 runs (If statement with EndIf):      3ms
Percentage Difference:                      90%

10,000 runs (single line If statement):     5.8ms
10,000 runs (If statement with EndIf):      3ms
Percentage Difference:                      93%

I'll write what makes the code more readable. Anyone having to depend on this, needs to rethink their code.  My 2 cents

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

I am also opinionated about single line statements. They decrease the readability often more than just a bit.

My recommendation for your specific case:
Try to optimise the algorithms, function logic instead of if-condition optimisation. I strongly believe there is more potential to optimize 😀 .

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
1 hour ago, SOLVE-SMART said:

Try to optimise the algorithms, function logic instead of if-condition optimisation. I strongly believe there is more potential to optimize

Thanks Sven. I agree with you 100%. Every time I look it over, I always find things that can be optimized or improved in various ways. I seem to actually enjoy this aspect of learning better ways to do things.

Normally I don’t worry too much about performance. But in this case it’s for a WinEventHook so it has to be able to handle everything that goes through the hook.

Posted
5 hours ago, SOLVE-SMART said:

Try to optimise the algorithms, function logic instead of if-condition optimisation. I strongly believe there is more potential to optimize 😀 .

Big Agree

I mean, it definitely helps if you're fighting a race condition but it's not a good idea to get yourself in one in the first place. 👀

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11, MSEdgeRedirect
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Posted

To break down the performance behavior in more detail: It depends on whether the condition is met or not.
The following test script:

Global Const $N = 1e7
Global $iT, $f_Overhead

ConsoleWrite(@CRLF & "            name             time" & @CRLF & "--------------------------------------" & @CRLF)

; determine the overhead of loop [and optional other stuff]
$iT = TimerInit()
For $i = 1 To $N
Next
$f_Overhead = TimerDiff($iT)

$iVariable = 1
$iT = TimerInit()
For $i = 1 To $N
    If $iVariable Then
        $iVariable = 1
    EndIf
Next
$iT = TimerDiff($iT)
ConsoleWrite(StringFormat("% 20s:\t%10.6f ms\n", "If-EndIf True", ($iT-$f_Overhead) / $N))

$iVariable = 0
$iT = TimerInit()
For $i = 1 To $N
    If $iVariable Then
        $iVariable = 1
    EndIf
Next
$iT = TimerDiff($iT)
ConsoleWrite(StringFormat("% 20s:\t%10.6f ms\n", "If-EndIf False", ($iT-$f_Overhead) / $N))

$iVariable = 1
$iT = TimerInit()
For $i = 1 To $N
    If $iVariable Then $iVariable = 1
Next
$iT = TimerDiff($iT)
ConsoleWrite(StringFormat("% 20s:\t%10.6f ms\n", "If-OneLiner True", ($iT-$f_Overhead) / $N))

$iVariable = 0
$iT = TimerInit()
For $i = 1 To $N
    If $iVariable Then $iVariable = 1
Next
$iT = TimerDiff($iT)
ConsoleWrite(StringFormat("% 20s:\t%10.6f ms\n", "If-OneLiner False", ($iT-$f_Overhead) / $N))

produces for me:

name             time
--------------------------------------
       If-EndIf True:     0.000260 ms
      If-EndIf False:     0.000161 ms
    If-OneLiner True:     0.000481 ms
   If-OneLiner False:     0.000088 ms

So it depends on whether the condition is met or not. If it is met, then the classic If-EndIf is faster. If it is not met, the one-liner is faster.
When conditions are balanced, their times will be roughly the same.

If you can estimate whether the vast majority of conditions are true or false, then you could certainly optimize on that basis. The relative differences are quite significant. But in absolute terms, they are completely subordinated. To put the measured numbers into perspective: we are talking about a difference between 160 nanoseconds and 88 nanoseconds – not milliseconds, not even microseconds! No, nanoseconds!
These are therefore overshadowed by any other parts of the code. (unless the script consists purely of millions of consecutive if queries).

This decision therefore makes no relevant difference to the overall script.

However, this makes the answer for this thread quite straightforward (as the others have already said): which of the two is used is independent of any performance considerations. The decision should be based solely on readability and clarity at the specific code location.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...