AutoHotkey Beginner Tip #2: SetBatchLines - Script Performance
Published: 2020-05-24 00:05:15

# Programming&#47;Scripting&#47;Coding Language Performance

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

# 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&#59; if you know how to use the language well&#46;

## Performance Using SetBatchLines

There are a few ways to optimize performance in AutoHotkey for window interaction&#44; key detection&#47;execution&#44; and what I am speaking about here&#58; script execution&#46; Mostly pertaining to loops and timers&#46;

One method to improve script performance is by using the &#39;SetBatchLines&#39; command&#46;

### 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&#44; but when doing things more intensive &#40;such as looping though indexed or associative arrays to execute&#47;increment code&#47;data&#41; this can slow down the script &#40;or timer&#47;function&#41; quite a bit with large indexed arrays&#46;

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.


```AutoHotkey
SetBatchLines,-1
; [-1] Or the number of (# of iterations+exec lines)
; in a loop
```

is EXTREMELY important for performance when doing any kind of looping or SetTimers &#40;maybe multithreading with DLL?&#41; etc&#46;&#46;&#46;

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&#46;g&#46;&#58;

```AutoHotkey
;; Initialize vars
maxRange:=100000
array := range(1,maxRange) ;; my range function
tmpIntVar:=0
```

Set each iteration:

```AutoHotkey
;; 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
}
```

or wrap the outside of the loop for smoothness:


```AutoHotkey
SetBatchLines,-1
loop, % array.MaxIndex()
{
    tmpIntVar++
}
 SetBatchLines,10ms
 ```

# Conclusion

The &#39;SetBatchLines&#39; 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&#46; With proper use of this command a program written in the language can be smooth&#46;