Jump to content

Are Local Const re-evaulated on every function call?


Recommended Posts

Consider the following snippet:

For $i=0 to 3
    testfunc1($i)
Next

For $i=0 to 3
    testfunc2($i)
Next

Func testfunc1($input)
     Local Const $msg = "hello " & string($input)
     MsgBox(0,"test1",$msg)
EndFunc

Func testfunc2($input)
     Local Static $msgHead = "hello "
     Local $msg = $msgHead & string($input)
     MsgBox(0,"test2",$msg)
EndFunc

Since local const does not strictly require literals, it is basically just an immutable local variable.

If the const in question is a relatively long string, and the function is called at very high frequencies, then wouldn't it in fact be more optimized to save the unchanging data in a Local Static?

Or is the AutoIt interpreter's abstractions automatically optimizing for the literal vs non-literal cases?

Link to comment
Share on other sites

I don't know the technical stuff on how AutoIt works, but I can test it with timers.

#include <Timers.au3>

$iTimer = _Timer_Init()
For $i=0 to 1000000
    testfunc1($i)
Next
$iDiff = _Timer_Diff($iTimer)

$iTimer2 = _Timer_Init()
For $i=0 to 1000000
    testfunc2($i)
Next
$iDiff2 = _Timer_Diff($iTimer2)

$iTimer3 = _Timer_Init()
For $i=0 to 1000000
    testfunc3($i)
Next
$iDiff3 = _Timer_Diff($iTimer3)

MsgBox(0, "", "Func1 Time: " & Round($iDiff/1000, 2) & " sec" & @CRLF & "Func2 Time: " & Round($iDiff2/1000, 2) & " sec" & @CRLF & "Func3 Time: " & Round($iDiff3/1000, 2) & " sec")

Func testfunc1($input)
     Local Const $msg = "hello " & string($input)
EndFunc

Func testfunc2($input)
     Local Static $msgHead = "hello "
     Local $msg = $msgHead & string($input)
EndFunc

Func testfunc3($input)
     Local Static $msg = "hello " & string($input)
EndFunc

I added a third function that mimics your first function exactly, but as a Static instead of a Constant. My best time was function #3. 

Func1 Time: 1.05 sec
Func2 Time: 1.37 sec
Func3 Time: 0.67 sec

Link to comment
Share on other sites

Interesting. I modified your benchmark a bit because, according to the documentations, test 3 won't alter the content on subsequent invocations:

 

#include <Timers.au3>

$str = ""
$iTimer = _Timer_Init()
For $i=0 to 1000000
    $str = testfunc1($i)
Next
$iDiff = _Timer_Diff($iTimer)

$str = ""
$iTimer2 = _Timer_Init()
For $i=0 to 1000000
    $str = testfunc2($i)
Next
$iDiff2 = _Timer_Diff($iTimer2)


MsgBox(0, "", "Func1 Time: " & Round($iDiff/1000, 2) & " sec" & @CRLF & "Func2 Time: " & Round($iDiff2/1000, 2) & " sec")

Func testfunc1($input)
     Local Const $msgHead  = "Et sed labore liber ex lorem praesent sed no tempor sea dolore erat vero ea amet facilisi. Quis diam iriure sed eros molestie sanctus. Amet ea dolore sit sea ipsum sed augue ipsum sit gubergren sadipscing accusam. Nisl gubergren vero illum eos nonumy dolor congue adipiscing sit amet. Gubergren takimata lorem tation dolor kasd delenit gubergren commodo ipsum clita stet congue lorem dolore ea ipsum. Nonumy ipsum dolore rebum sit labore sit takimata wisi blandit duo. Ea est sit rebum consetetur facer eirmod molestie autem quis at lorem dolor accusam lorem. Et duo accusam dolor ut tempor ullamcorper ipsum vel amet est. Et consetetur amet sit lorem sed eirmod at dolores ea consetetur. Dolores te luptatum kasd aliquip sed clita et ut est aliquam imperdiet dolor et invidunt. Ipsum consetetur ut ipsum illum duo stet ipsum sed feugiat esse vero labore nulla commodo. In erat vel suscipit. Sit et vulputate illum dolor nibh diam sed eirmod blandit duo. "
     Return $msgHead & string($input)
EndFunc

Func testfunc2($input)
     Local Static $msgHead = "Et sed labore liber ex lorem praesent sed no tempor sea dolore erat vero ea amet facilisi. Quis diam iriure sed eros molestie sanctus. Amet ea dolore sit sea ipsum sed augue ipsum sit gubergren sadipscing accusam. Nisl gubergren vero illum eos nonumy dolor congue adipiscing sit amet. Gubergren takimata lorem tation dolor kasd delenit gubergren commodo ipsum clita stet congue lorem dolore ea ipsum. Nonumy ipsum dolore rebum sit labore sit takimata wisi blandit duo. Ea est sit rebum consetetur facer eirmod molestie autem quis at lorem dolor accusam lorem. Et duo accusam dolor ut tempor ullamcorper ipsum vel amet est. Et consetetur amet sit lorem sed eirmod at dolores ea consetetur. Dolores te luptatum kasd aliquip sed clita et ut est aliquam imperdiet dolor et invidunt. Ipsum consetetur ut ipsum illum duo stet ipsum sed feugiat esse vero labore nulla commodo. In erat vel suscipit. Sit et vulputate illum dolor nibh diam sed eirmod blandit duo. "
     Return $msgHead & string($input)
EndFunc

 

Which produced the following result:

image.png.0f377de4e4dae2b225912f24b3b5ea5d.png

Func1 Time: 5.02 sec

Func2 Time: 4.69 sec

So it seems that skipping the interpretation of the Static line entirely does produce a minor benefit over many, many iterations.

 

EDIT: a few more tests:

image.png.e7ce1d493c7776f0902d9ebde54b575d.png

Edited by AutoXenon
Link to comment
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
 Share

  • Recently Browsing   0 members

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