Jump to content

Recommended Posts

  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Example 1: better to just overwrite (A) the variable (same memory location, so the number of (re)writes doesn't matter). But every extra function call costs performance. Example 2: Fastest eval

BTW AutoIt arrays are limited to only 16 millions cells, while databases are essentially unbounded in size (I've [remotely] approached an SQLite DB totalling 120Tb) and they offer unrivaled possibilit

I' am not sure if this is a robust test ? Global $hTimer, $bBoolean, $iMax = 5000000 $hTimer = TimerInit() For $i = 1 To $iMax $bBoolean = False Next ConsoleWr

Function arguments that are not ByRef are copies of the original argument send to the function right?
so wouldn't using ByRef be better for memory management and speed most of the time? Especially with large arrays or big strings that need to be passed around.
A lot of examples and functions in autoit don't use byref by default.

Link to post
Share on other sites
2 hours ago, TheAutomator said:

Function arguments that are not ByRef are copies of the original argument send to the function right?

[...]

A lot of examples and functions in autoit don't use byref by default.

Yes, but ByRef is not always required for arrays to avoid local copies ! This is described very comprehensively in Global,_Local,_Static_and_ByRef

Excerpt :

"It was mentioned earlier that when you pass variables to functions AutoIt makes local copies of these variables unless you declare that they are to be passed ByRef. In the special case of arrays, which are typically larger than a simple variable, AutoIt will only make a local copy if you change the array inside the function - merely accessing the array will use the original version. So passing the array ByRef is only advantageous if you intend to modify the array within the function - however, it does no harm to force the issue by using the keyword. "

Full text :

Spoiler

Passing variables as parameters using ByRef

When you call a function in AutoIt, you can pass parameters to the function by including them in the parentheses following the function name like this - Function(Parameter_1, Parameter_2). Passing variables in this way means that you can use variables that have been declared as Local within other functions. Note that the parameters are automatically declared as Local variables within the called function and so do not need to be declared again in the code.

Variables can be passed as parameters to a function in 2 ways:

By Value - This is the default. The parameter is treated as a Local variable within the function to which it is passed and any changes made to it are lost when the function ends. So no changes are made to the Local variable within the original function.

By Reference - This is used when ByRef is added before the variable name in the parameter list of the function declaration. Now any changes to the variable made in the function to which it is passed also affect the Local variable in the original function. If you do not understand why this is such a powerful capability, you need to read the Global/Local section again!

Here is a small example to show how parameters are passed when calling a function:


#include <MsgBoxConstants.au3> ; only required for MsgBox constants

Func_A()

Func Func_A()
	; Declare variables
	Local $iVar_A1 = 10
	Local $iVar_A2 = 20
	; And read them
	MsgBox($MB_SYSTEMMODAL, "Read", "Initial read in Func A:" & @CRLF & "Var A1 = " & $iVar_A1 & @CRLF & "Var A2 = " & $iVar_A2)

	; Now pass these variables to another function - we use the names we have already declared in this function
	Func_B($iVar_A1, $iVar_A2)

	; We changed the parameters in Func_B - but nothing happened to the variables in this function
	MsgBox($MB_SYSTEMMODAL, "Read", "Second read in Func A:" & @CRLF & "Var A1 = " & $iVar_A1 & @CRLF & "Var A2 = " & $iVar_A2)
EndFunc   ;==>Func_A

Func Func_B($iVar_B1, $iVar_B2) ; There is no need to declare the variables as parameters are automatically Local in scope
	; Now read these variables - note that they have the same value as the variables in Func_A
	MsgBox($MB_SYSTEMMODAL, "Read", "Initial read in Func B:" & @CRLF & "Var B1 = " & $iVar_B1 & @CRLF & "Var B2 = " & $iVar_B2)

	; Let us change them
	$iVar_B1 = 100
	$iVar_B2 = 200

	; And confirm that they have changed
	MsgBox($MB_SYSTEMMODAL, "Read", "Second read in Func B:" & @CRLF & "Var B1 = " & $iVar_B1 & @CRLF & "Var B2 = " & $iVar_B2)

	; Now return to the other function
EndFunc   ;==>Func_B

As you can see, the Local variables within Func_A are read perfectly by Func_B when passed as parameters, even though they were declared as Local in Func_A. However, although Func_B changes the value of the variables, the values of the original variables within Func_A are not changed.

If we want to change the value of the variables in Func_A from within Func_B, we need to use the ByRef keyword as explained above and illustrated here:


#include <MsgBoxConstants.au3> ; only required for MsgBox constants

Func_A()

Func Func_A()
	; Declare variables
	Local $iVar_A1 = 10
	Local $iVar_A2 = 20

	; And read them
	MsgBox($MB_SYSTEMMODAL, "Read", "Initial read in Func A:" & @CRLF & "Var A1 = " & $iVar_A1 & @CRLF & "Var A2 = " & $iVar_A2)

	; Now pass these variables to another function - but this time we will pass $iVar_A2 By Reference
	Func_B($iVar_A1, $iVar_A2)

	; We changed the parameters in Func_B - this time we see that we have changed $iVar_A2
	MsgBox($MB_SYSTEMMODAL, "Read", "Second read in Func A:" & @CRLF & "Var A1 = " & $iVar_A1 & @CRLF & "Var A2 = " & $iVar_A2)
EndFunc   ;==>Func_A

Func Func_B($iVar_B1, ByRef $iVar_B2) ; Note the ByRef keyword in the function declaration for the second parameter
	; Now read these variables - they have the same value as the variables in Func_A
	MsgBox($MB_SYSTEMMODAL, "Read", "Initial read in Func B:" & @CRLF & "Var B1 = " & $iVar_B1 & @CRLF & "Var B2 = " & $iVar_B2)

	; Let us change them
	$iVar_B1 = 100
	$iVar_B2 = 200

	; And confirm that they have changed
	MsgBox($MB_SYSTEMMODAL, "Read", "Second read in Func B:" & @CRLF & "Var B1 = " & $iVar_B1 & @CRLF & "Var B2 = " & $iVar_B2)

	; Now return to the other function
EndFunc   ;==>Func_B

A couple of more advanced points:

- It was mentioned earlier that when you pass variables to functions AutoIt makes local copies of these variables unless you declare that they are to be passed ByRef. In the special case of arrays, which are typically larger than a simple variable, AutoIt will only make a local copy if you change the array inside the function - merely accessing the array will use the original version. So passing the array ByRef is only advantageous if you intend to modify the array within the function - however, it does no harm to force the issue by using the keyword.

- If you are dealing with a simple variable of considerable size (the text of a file for example) then passing it ByRef is a sensible action. But if you want to make sure that the variable cannot be altered inside the function, you can declare it as Const ByRef in the function definition as shown here:


Local $sString = "A very large chunk of data which we do not want to change"

Foo($sString)

Func Foo(Const ByRef $sText) ; The data is not copied, but also cannot be changed
	; Uncomment this line to see what happens when we try
	;$sText = "New value"
EndFunc   ;==>Foo

ByRef is a very powerful feature - but use it carefully, or you may find that you have changed a variable when you did not want to!

 

 

Edited by Musashi
insert a spoiler instead of a quote

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

Different syntaxes, different ways to waste cycles.  In order of increasing waste:

Global $hTimer, $bBoolean, $iMax = 5000000

$hTimer = TimerInit()
For $i = 1 To $iMax
    $bBoolean = 0
Next
ConsoleWrite("1. Time = " & TimerDiff($hTimer) & @CRLF)

$hTimer = TimerInit()
For $i = 1 To $iMax
    $bBoolean = False
Next
ConsoleWrite("2. Time = " & TimerDiff($hTimer) & @CRLF)

$hTimer = TimerInit()
For $i = 1 To $iMax
    $bBoolean = Not True
Next
ConsoleWrite("3. Time = " & TimerDiff($hTimer) & @CRLF)

$hTimer = TimerInit()
For $i = 1 To $iMax
    $bBoolean = Not(True)
Next
ConsoleWrite("4. Time = " & TimerDiff($hTimer) & @CRLF)

$hTimer = TimerInit()
For $i = 1 To $iMax
    $bBoolean = (1 = 0)
Next
ConsoleWrite("5. Time = " & TimerDiff($hTimer) & @CRLF)

AutoIt being an interpreter, it's quite logical that the more involved an expression is, the more work it requires at every invokation.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

I will add that readability is important.
For example: $MB_OKCANCEL (1) "OK and Cancel". Using 1 will be faster but less readable.
Nowadays, speed, flip-flops, however you look at it, is not as the 286 CPU era.
So these questions are good but do not sacrifice readability. Today's CPUs are quite fast :) 

Edited by argumentum
Link to post
Share on other sites
49 minutes ago, jchd said:

Different syntaxes, different ways to waste cycles.  In order of increasing waste:

AutoIt being an interpreter, it's quite logical that the more involved an expression is, the more work it requires at every invokation.

hmm, strange..

1. Time = 4513.5517
2. Time = 4327.9589
3. Time = 5918.9678
4. Time = 7456.2174
5. Time = 8699.0866

test 2 has to first convert False to 0 according to a previous answer, but it takes less time?

Edited by TheAutomator
Link to post
Share on other sites
7 hours ago, Musashi said:

Yes, but ByRef is not always required for arrays to avoid local copies ! This is described very comprehensively in Global,_Local,_Static_and_ByRef

Excerpt :

"It was mentioned earlier that when you pass variables to functions AutoIt makes local copies of these variables unless you declare that they are to be passed ByRef. In the special case of arrays, which are typically larger than a simple variable, AutoIt will only make a local copy if you change the array inside the function - merely accessing the array will use the original version. So passing the array ByRef is only advantageous if you intend to modify the array within the function - however, it does no harm to force the issue by using the keyword. "

How did i miss that? 😅
Thanks for the research!

Link to post
Share on other sites
Just now, argumentum said:

...yeah, CPUs "cheat". If a calculation is been done more than a few times, is just replied without the calculation as the answer is known, so, follow the logic. ;) 

nice, didn't know that,
but then it has to be the same for the other ones right? especially for the more easy one (1)

Link to post
Share on other sites

May 12-y old CPU(*) has different timings:

1. Time = 1346.66836591923
2. Time = 1419.11255965179
3. Time = 2037.54539549819
4. Time = 2592.13496621027
5. Time = 3133.78265580193

EDIT:
(*) Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
Please don't laugh!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites
4 minutes ago, TheAutomator said:

then it has to be the same for the other ones right?

..is a muti-threaded OS. It all depends on what the OS gives as a time slice at the time. Hence, follow the logic. Can't go wrong there.

Edit: here are my results on a 8700k

1. Time = 632.262649350649
2. Time = 673.552346320346
3. Time = 1034.81406060606
4. Time = 1296.44993939394
5. Time = 1536.13021645022

 

Edited by argumentum
Link to post
Share on other sites

My new 256-cubit quantum computer gives Time = 0.0 for all five.:geek: What gives?:huh:

Link to post
Share on other sites

...another thing to keep in mind is the time it takes for ...something else. A PC is as fast as the slowest thing.
Say that a disk write, in a slow disk, goes on while your loop is running, in the middle of it. The slice of time is gonna be longer, due to the process writing to disk.

So you may have a fast CPU but a slow HDD will be the bottle neck. Therefore, for some stuff I use a RAM disk. If you have a NVMe drive then ... :thumbsup:

Knowing software without understanding hardware will not do for a purist/perfectionist. I come from hardware then software came into existence. I guess that in a computer holy book would start with "In the beginning there was nothing, then a guy created a cable permitting electric potentials to flow..:D

It's handy to understand the hardware the software runs on. Otherwise is a mysterious enterprise.

Link to post
Share on other sites

Argumentum,

I do my best to learn as much as possible and yes i still have much to learn, i'm sure you know not everyone on this forum started as a computer scientist 😉
Everything i know i had to teach myself, everything i code i want to program as smooth and efficient as possible.
Focusing on even the little details.. that's what i mean with perfectionism.

Sometimes the more you learn, the more you discover how little you know, so I'm grateful for all the tips and help :)

Edited by TheAutomator
Link to post
Share on other sites

For the sake of completeness:
The wiki and the forum are a great source for good and efficient coding pactices :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites
  • 2 years later...

Yet another question:

Why do we use sleep(100) inside an "idle" loop?
Does it make the loop more CPU friendly, and how?
I would guess you're giving the CPU even more work like this? (read somewhere that sleep even does tiny sleeps with breaks to check for hotkey-presses)

example from helpfile in *GUI Reference - OnEvent Mode

While 1
    sleep(100)  ; Sleep to reduce CPU usage
Wend

ps: hopefully not breaking any rules by putting such a big time between new questions related to this topic..

Link to post
Share on other sites

imho Sleep(10) is much better than Sleep(100) because it got exactly the same effect on CPU, i.e. it won't increase CPU usage at all and it won't increase its heat (task manager shows it on my antique PC)

Without Sleep(10) it's a total different story and CPU usage is increasing like crazy.

That's why, I guess, there's an implicit Sleep(10) in the instruction "Switch GUIGetMsg()" often found within the While...WEnd loop, which prevents CPU overheating.
 

Link to post
Share on other sites
On 4/21/2022 at 4:16 AM, pixelsearch said:

imho Sleep(10) is much better than Sleep(100) because it got exactly the same effect on CPU, i.e. it won't increase CPU usage at all and it won't increase its heat (task manager shows it on my antique PC)

Without Sleep(10) it's a total different story and CPU usage is increasing like crazy.

That's why, I guess, there's an implicit Sleep(10) in the instruction "Switch GUIGetMsg()" often found within the While...WEnd loop, which prevents CPU overheating.
 

But, what makes it so that the CPU heats up?
What's the difference between "check while condition over and over again", or "check while condition, sleep millisecond, sleep millisecond, sleep millisecond, back to while condition..."?

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

    No registered users viewing this page.

  • Similar Content

    • By Zaoka
      HI,
      for couple of years I'm using  Jos script for sending reports, email with excel attachment. But from last week i'm getting this error when sending excel or word attachment
      message has lines too long for transport jpeg, pdf works with no problems, any sugestion ?
    • By diepfeile
      I'm using the following:
      Autoit 3.3.14.5
      newly installed Beta 3.3.15.5
      SQlite version 3380000 aka 3.38.0
      I put sqlite3.dll and sqlite3_x64.dll in C:\Windows\System32 since many scripts depend on them.


      I extended the output of _SQLite_Startup()
      with:
      ConsoleWrite("@AutoItX64 " & @AutoItX64 & @CRLF) ConsoleWrite("$sDll_Filename " & $sDll_Filename & @CRLF) ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)

      Also using the script from https://www.autoitscript.com/autoit3/docs/libfunctions/_SQLite_Startup.htm for testing.

       
      >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "R:\Download\aasdf.au3" @AutoItX64 0 $sDll_Filename sqlite3.dll _SQLite_LibVersion=0 >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "R:\Download\aasdf.au3" @AutoItX64 1 $sDll_Filename sqlite3_x64.dll _SQLite_LibVersion=3.38.0 >Running:(3.3.15.5):C:\Program Files (x86)\AutoIt3\Beta\autoit3.exe "R:\Download\aasdf.au3" @AutoItX64 0 $sDll_Filename sqlite3.dll _SQLite_LibVersion=0 >Running:(3.3.15.5):C:\Program Files (x86)\AutoIt3\Beta\autoit3_x64.exe "R:\Download\aasdf.au3" @AutoItX64 1 $sDll_Filename sqlite3_x64.dll _SQLite_LibVersion=3.38.0


      Why doesn't it work in 32bit, despite me having the 32bit sqlite.dll? Autoit urges running scripts in 32bit mode and Scite starts scripts just in 32bit mode without the flag?
      With #AutoIt3Wrapper_UseX64=Y it just works, both normal Autoit and beta!
      sqlite3.dll sqlite3_x64.dll
    • By Iraj
      Hello Team, Greetings!
      Is there any way to run any 3rd party application silently in background without the GUI getting in the front ?
      I was implementing angry-ip scanner with autoit & wanted the angry-ip application to run in background quietly as I am copying its output to other file on completion. is there any way to achieve my query?
      Below is code I tried: 
      $range = "192.168.0.1 192.168.0.255" ShellExecuteWait("C:\Windows\DDM\ipscan.exe","-f:range "&$iprange&" -q -o C:\temp\ScanResults.csv","","open",@SW_HIDE) Thanks!
    • By PeterVerbeek
      This topic give you access to an AutoIt functions library I maintain which is called PAL, Peter's AutoIt Library. The latest version 1.26 contains 214 functions divided into these topics:
      window, desktop and monitor GUI, mouse and color GUI controls including graphical buttons (jpg, png) logics and mathematics include constants string, xml string and file string dialogues and progress bars data lists: lists, stacks, shift registers and key maps (a.ka. dictionaries) miscellaneous: logging/debugging, process and system info Change log and files section  on the PAL website (SourceForge).
      A lot of these functions were created in the development of Peace, Peter's Equalizer APO Configuration Extension, which is a user interface for the system-wide audio driver called Equalizer APO.
    • By Hermes
      Hi, I am struggling in setting the value of a textarea based on the value of clipboard (that contains a long web page source codes). If I use _WD_SetElementValue, it freezes after some time, or appears to be pressing tab and goes out of focus. I can also use send keys but i need the script to run in the background.
      Here is the full script:
      #Include "Chrome.au3" #Include "wd_core.au3" #Include "wd_helper.au3" #Include "WinHttp.au3" #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <AutoItConstants.au3> #include <WinAPIFiles.au3> #include <GDIPlus.au3> #include <Excel.au3> Local $sDesiredCapabilities, $sSession SetupChrome() _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_LoadWait($sSession) _WD_Navigate($sSession, "http://demo.borland.com/testsite/stadyn_largepagewithimages.html") _WD_LoadWait($sSession) Global $sSource = _WD_GetSource($sSession) Local $Paste = ClipPut($sSource) Local $sData = ClipGet() Local $aArray = 0, _ $iOffset = 1 While 1 $aArray = StringRegExp($sData, '(?s)<p>.*</p>', $STR_REGEXPARRAYMATCH, $iOffset) If @error Then ExitLoop $iOffset = @extended For $i = 0 To UBound($aArray) - 1 Local $Paste = ClipPut($aArray[$i]) Local $sRegExData = ClipGet() ;MsgBox(0, "", "$sRegExData = " & $sRegExData) Next WEnd _WD_Navigate($sSession, "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_textarea_placeholder") _WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult") Local $sElement1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByCSSSelector, "iframe#iframeResult") _WD_FrameEnter($sSession, $sElement1) _WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//html/body/textarea") $textarea = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//html/body/textarea") _WD_ElementAction($sSession, $textarea, 'click') ;WD SetElementValue(SsSession, Stextarea, $sRegExData) <-- I can do this but the focus goes out, or the browser freezes _WD_FrameLeave($sSession) sleep(2000) Send("^v") _WD_LoadWait($sSession) _WD_Shutdown() Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}' EndFunc ;==>SetupChrome Can someone help me please, or re-direct me to the right path? TIA!
×
×
  • Create New...