DEV Community

Ian Pride
Ian Pride

Posted on • Updated on

AutoHotkey Beginner Tip #2: SetBatchLines - Script Performance

Programming/Scripting/Coding Language Performance

I won't be getting deep into the performance of programming languages (as that would draw debates I'm not ready to get into ­čśü), but if you are familiar with the subject then you probably understand that higher level languages (unless compiled sometimes) can be slower in some areas of computing compared to lower level languages like the C family and Assembly (among others) etc; though this doesn't always matter much anymore as CPU's got faster and RAM got bigger. This, of course, is not true for stuff you want to be doing at, say, NASA ­čśü.

AutoHotkey - Performance

AutoHotkey is by far my favorite language to code in Windows for various reasons for most everyday tasks from simple routine automation to full-on interactive programs with GUIs and in my opinion it compares to other languages of its kind when it comes to speed; if you know how to use the language well.

Performance Using SetBatchLines

There are a few ways to optimize performance in AutoHotkey for window interaction, key detection/execution, and what I am speaking about here: script execution. Mostly pertaining to loops and timers.

One method to improve script performance is by using the 'SetBatchLines' command.

Use SetBatchLines Command

By default AutoHotkey executes lines for 10ms and then sleeps for 10ms and this is great for misc default profile scripts or simple automation tasks, but when doing things more intensive (such as looping though indexed or associative arrays to execute/increment code/data) this can slow down the script (or timer/function) quite a bit with large indexed arrays.

Excerpt from AutoHotkey Docs:

Determines how fast a script will run (affects CPU utilization).
SetBatchLines, 20ms
SetBatchLines, LineCount

Parameters

20ms
(The 20ms is just an example.) If the value ends in ms, it indicates how often the script should sleep (each sleep is 10 ms long). In the following example, the script will sleep for 10ms every time it has run for 20ms: SetBatchLines, 20ms.

LineCount
The number of script lines to execute prior to sleeping for 10ms. The value can be as high as 9223372036854775807. Also, this mode is mutually exclusive of the 20ms mode in the previous paragraph; that is, only one of them can be in effect at a time.

SetBatchLines,-1
; [-1] Or the number of (# of iterations+exec lines)
; in a loop
Enter fullscreen mode Exit fullscreen mode

is EXTREMELY important for performance when doing any kind of looping or SetTimers (maybe multithreading with DLL?) etc...

You can either run the whole program with no sleep in its execution by placing the command at the top of a script or you can turn it on and off inside the loop for iteration precision control e.g.:

;; Initialize vars
maxRange:=100000
array := range(1,maxRange) ;; my range function
tmpIntVar:=0
Enter fullscreen mode Exit fullscreen mode

Set each iteration:

;; Set and reset each iteration
for index, item in array
{
    SetBatchLines,-1 ;; set befeore execution
    ; or SetBatchLines,% (maxRange+3)
    tmpIntVar++ ;; do something
    SetBatchLines,10ms ;; default value
}
Enter fullscreen mode Exit fullscreen mode

or wrap the outside of the loop for smoothness:

SetBatchLines,-1
loop, % array.MaxIndex()
{
    tmpIntVar++
}
 SetBatchLines,10ms
Enter fullscreen mode Exit fullscreen mode

Conclusion

The 'SetBatchLines' command is essential for controlling the flow and speed of a script and I believe a scource of confusion when it comes to why some people may think the language is slow or inefficient. With proper use of this command a program written in the language can be smooth.

Discussion (2)

Collapse
cajotafer profile image
Carlos Fernández

Interesting article. I didn't know about AutoHotkey but now that I know this I want to learn about it.

Collapse
thefluxapex profile image
Ian Pride Author • Edited on

It's a Windows high-level programming/scripting language that has sort of a bad reputation as a game cheating tool, but there are more cheat tools written in C and Assembly than any other language. People will also tell you there are better languages (like Python), but I prefer AHK over all! I can write the same full gui program in AutoHotkey and Python and the AHK version is 1-2MB while the super refactored Python version with all extras stripped will still be 15-20mbs and the AHK version will run faster half of the time no matter how much you refactor. AHK is a POWERFUL language for windows programming/automation.