Page 6 of 7

Re: How to optimize the speed of a script as much as possible.

Posted: 03 Dec 2019, 16:36
by swagfag
dude, just start a new help thread and go over ur issue there, code and everything
99% the root cause has nothing at all to do with optimizations, setbatchlines, cpu cores, percentages, multithreading

Re: How to optimize the speed of a script as much as possible.

Posted: 24 Jan 2020, 17:27
by hellas
Sam_ wrote:
29 Nov 2019, 15:34
mojobadshah wrote:
29 Nov 2019, 14:26
What is the specific mechanism I need to initiate through AHK to push CPU rate up to at least x2 or ~50% or even more than this ?
AHK is single threaded. Each instance of a running script will only ever use a single CPU core. You can SetBatchLines to -1 and change the process priority to above normal, but beyond that there isn't much you can do to force AHK to utilize more resources in order to run faster. If you haven't already, reading the Performance section of RegExReplace might be insightful.
Why not High process priority? why "Avobenormal" is the best option?

Re: How to optimize the speed of a script as much as possible.

Posted: 24 Jan 2020, 18:02
by Sam_
hellas wrote:
24 Jan 2020, 17:27
Sam_ wrote:
29 Nov 2019, 15:34
mojobadshah wrote:
29 Nov 2019, 14:26
What is the specific mechanism I need to initiate through AHK to push CPU rate up to at least x2 or ~50% or even more than this ?
AHK is single threaded. Each instance of a running script will only ever use a single CPU core. You can SetBatchLines to -1 and change the process priority to above normal, but beyond that there isn't much you can do to force AHK to utilize more resources in order to run faster. If you haven't already, reading the Performance section of RegExReplace might be insightful.
Why not High process priority? why "Avobenormal" is the best option?
SvenBent wrote:
26 Mar 2016, 14:34
May i put in a small warning against "Process, Priority, , H" especially for big CPU heavy scripts

Process, Priority, , H puts you priority in the group with keyboard input and network drivers. So any CPU heavy sections of the script is going to infuse lag on both KB and network.
However if you script does not have any CPU heavy.sections there is no real benefit from adjusting priory. may i instead suggest do do only "Process, Priority, , A" it will still keep you priority above any other running software but below network and keyboard. Since Windows priority model is blocking priority ( With a lot of exceptions) there is no difference from being in High vs Above, when all other software is in Normal anyway. ( aka multiple steps higher priority is just as good as one step higher)

in short: unless you have a very specific technically reason. use only "Process, Priority, , A" to increase script priority.

Optimizing Loop

Posted: 20 Mar 2021, 07:39
by serg
I was wondering how much "Loop" slows things down. So I tested it in comparison with simple math funcs. Maybe somebody will find this useful in optimizing their scripts.

First test, 30 additions of 1 var:

Code: Select all

t := A_TickCount
Loop 100000
 Loop 30
  a%a_index%++
t := A_TickCount - t

tt := A_TickCount
Loop 100000
 a1++ , a2++ , a3++ , a4++ , a5++ , a6++ , a7++ , a8++ , a9++ , a10++ , a11++ , a12++ , a13++ , a14++ , a15++ , a16++ , a17++ , a18++ , a19++ , a20++ , a21++ , a22++ , a23++ , a24++ , a25++ , a26++ , a27++ , a28++ , a29++ , a30++
tt := A_TickCount - tt
msgbox % "Time in milisec:`n" t "`n" tt "`n`nSpeed difference = " Round(t/tt,1)
Second test, 30 additions of 4 vars:

Code: Select all

t := A_TickCount
Loop 30000
 Loop 30
  a%a_index%++ , b%a_index%++ , c%a_index%++ , d%a_index%++
t := A_TickCount - t

tt := A_TickCount
Loop 30000
 a1++ , b1++ , c1++ , d1++ , a2++ , b2++ , c2++ , d2++ , a3++ , b3++ , c3++ , d3++ , a4++ , b4++ , c4++ , d4++ , a5++ , b5++ , c5++ , d5++ , a6++ , b6++ , c6++ , d6++ , a7++ , b7++ , c7++ , d7++ , a8++ , b8++ , c8++ , d8++ , a9++ , b9++ , c9++ , d9++ , a10++ , b10++ , c10++ , d10++ , a11++ , b11++ , c11++ , d11++ , a12++ , b12++ , c12++ , d12++ , a13++ , b13++ , c13++ , d13++ , a14++ , b14++ , c14++ , d14++ , a15++ , b15++ , c15++ , d15++ , a16++ , b16++ , c16++ , d16++ , a17++ , b17++ , c17++ , d17++ , a18++ , b18++ , c18++ , d18++ , a19++ , b19++ , c19++ , d19++ , a20++ , b20++ , c20++ , d20++ , a21++ , b21++ , c21++ , d21++ , a22++ , b22++ , c22++ , d22++ , a23++ , b23++ , c23++ , d23++ , a24++ , b24++ , c24++ , d24++ , a25++ , b25++ , c25++ , d25++ , a26++ , b26++ , c26++ , d26++ , a27++ , b27++ , c27++ , d27++ , a28++ , b28++ , c28++ , d28++ , a29++ , b29++ , c29++ , d29++ , a30++ , b30++ , c30++ , d30++
tt := A_TickCount - tt
msgbox % "Time in milisec:`n" t "`n" tt "`n`nSpeed difference = " Round(t/tt,1)
In first test, speed difference came to be ~3.5, in second ~5, which means that loop (inner one) + resolving var references (a%a_index%>a[1,2..]) consumed 3.5-5 times more time than actual calculations! Clearly this difference is getting less as complexity of calculation increases. Still, in some situations where Loop has only calculations inside without "if/break/continue...", and performance matters (as it does in my project), replacing Loop like in example above speeds things a lot.

Obviously, no need to rewrite things manually, in the example above I used this:

Code: Select all

q := ""
Loop 30
 q .= "a" a_index "++ , b" a_index "++ , c" a_index "++ , d" a_index "++ , "
clipboard := q

Re: How to optimize the speed of a script as much as possible.

Posted: 20 Mar 2021, 13:54
by Xtra
Performance: [v1.0.48+]: The comma operator is usually faster than writing separate expressions, especially when assigning one variable to another (e.g. x:=y, a:=b). Performance continues to improve as more and more expressions are combined into a single expression; for example, it may be 35% faster to combine five or ten simple expressions into a single expression.
See: operators in expressions

Re: How to optimize the speed of a script as much as possible.

Posted: 22 Mar 2021, 06:12
by serg
Re @Xtra
You are right, part of speed improvement came from use of comma.
Same test as above but without commas:

Code: Select all

t := A_TickCount
Loop 100000
 Loop 30
  a%a_index%++
t := A_TickCount - t

tt := A_TickCount
Loop 100000 {
 a1++
 a2++
 a3++
 a4++
 a5++
 a6++
 a7++
 a8++
 a9++
 a10++
 a11++
 a12++
 a13++
 a14++
 a15++
 a16++
 a17++
 a18++
 a19++
 a20++
 a21++
 a22++
 a23++
 a24++
 a25++
 a26++
 a27++
 a28++
 a29++
 a30++
 }
tt := A_TickCount - tt
msgbox % "Time in milisec:`n" t "`n" tt "`n`nSpeed difference = " Round(t/tt,1)
without commas speed difference came down from ~5 to ~3

Re: How to optimize the speed of a script as much as possible.

Posted: 23 Sep 2021, 12:43
by 20170201225639
> Gosub VS functions
> SvenBent wrote:
> This is probably way down into miniscule area but
> Using Gosub seems to be 25% faster than using a function.

I wonder if this is still true. I recently tested function call vs. gosub vs. method call wrt efficiency. Function call indisputably came out on top (I expected gosub to be faster)

Re: How to optimize the speed of a script as much as possible.

Posted: 30 Nov 2021, 04:37
by Gildum
WAZAAAAA wrote:
14 Feb 2015, 14:00
6. Even though SendInput ignores SetKeyDelay, SetMouseDelay and SetDefaultMouseSpeed, having these delays at -1 improves SendEvent's speed just in case SendInput is not available and falls back to SendEvent.
I just did a speed test with mousemove commands and SendInput actually does not ignore SetMouseDelay and SetDefaultMouseSpeed. So it's actually necessary to include SetMouseDelay -1
and SetDefaultMouseSpeed 0 for maximum script speed. I'm on Autohotkey v1.1.33.10.

Thanks for all the helpful optimization tips everyone!

Re: How to optimize the speed of a script as much as possible.

Posted: 25 Dec 2021, 14:33
by Fidanza
WAZAAAAA wrote:
14 Feb 2015, 14:00

DllCall("Sleep",UInt,17) ;I just used the precise sleep function to wait exactly 17 milliseconds[/code]
hey the precision of this script has gone after windows 10 1803.. :!: :problem: i mean with windows 1809 and later windows 10 now uses QueryPerformanceFrequency as a fixed 10MHZ value and because of this the precision and speed of this command vanished..and i really dont understand why nobody aware of this problem.. :roll: the same problem happening with steelseries keyboard macros..macros work but slower than before on windows 10 1809 and later.. anybody know this or is there a work around to fix this ? thnx

Re: How to optimize the speed of a script as much as possible.

Posted: 05 Feb 2022, 19:49
by amateur+
#1
a .= "c" vs this.a .= "c"
Spoiler

#2
VarSetCapacity(a, 2*iterations) vs none (auto-expanding VarSetCapacity(a, 0)) in concatenation a .= "c" test.
Spoiler

#3
a++ vs this.a++
Spoiler

#4
a++ vs this.Add1(a) test.
Add1(ByRef n) {
n++
}
Spoiler

#5
a++ vs not ByRef a := this.Add1(a) test.
Add1(n) {
return ++n
}
Spoiler

#6
a .= "c" vs ByRef this.ConcatenateC(b)
ConcatenateC(ByRef str) {
str .= "c"
}
Spoiler

#7
a .= "c" vs Not ByRefb := this.ConcatenateC(b)
ConcatenateC(str) {
return str .= "c"
}
Spoiler

#8
a .= "c" vs Not ByRef, Not inside a class b := ConcatenateC(b)
ConcatenateC(str) {
return str .= "c"
}
Spoiler

#9
a .= "c" vs ByRef, Not inside a class ConcatenateC(b)
ConcatenateC(ByRef str) {
str .= "c"
}
Spoiler

Re: How to optimize the speed of a script as much as possible.

Posted: 06 Feb 2022, 11:40
by ScripterBoi
What is the Tick Count?(im dumb)
Ive noticed that SendPlay's Tick Count is the lowest one

Re: How to optimize the speed of a script as much as possible.

Posted: 06 Feb 2022, 13:27
by amateur+
@ScripterBoi, you can read about it here: TickCount.

Re: How to optimize the speed of a script as much as possible.

Posted: 06 Feb 2022, 20:56
by Sam_
@amateur+
The more layers of obfuscation you add, the more processor operations and thus the slower AHK will be. Embedding "variables" within objects or classes adds layers of obfuscation. Manipulating a simple variable will ALWAYS be slower than performing an equivalent operation within an object or class. Calling a function directly will always be quicker than calling an equivalent method in a class. This shouldn't be surprising, and this won't change no matter what the operation is.

Re: How to optimize the speed of a script as much as possible.

Posted: 06 Feb 2022, 21:02
by amateur+
@Sam_, yes, but it is interesting to see some figures.

Re: How to optimize the speed of a script as much as possible.

Posted: 06 Feb 2022, 21:26
by guest3456
amateur+ wrote:
06 Feb 2022, 21:02
@Sam_, yes, but it is interesting to see some figures.
its not really interesting at all, until you provide something that would benchmark opposite from what our common sense would tell us.

so far, we all expect that less complexity to be faster than more complexity, and thats all you've trivially shown

Re: How to optimize the speed of a script as much as possible.

Posted: 06 Feb 2022, 21:44
by amateur+
@guest3456, and you really expected not ByRef concatenation to be 100+ times less effective than ByRef one?
Also did you expected the result from #1 (here)?
I've just noticed that my timer subroutine inside a class set up for 100 ms updates far less frequently than every 100 ms and even 200 ms. I ran some tests to figure out what degree of influence some nuances have and decided to share these tests.
But I think I should gather them in one post instead of spamming. Maybe tomorrow I'll put them all in one post and delete the others.

EDIT: Done. Now moderators are encouraged to remove "cleared" posts since I'm not able to do that on my own.
[Mod edit: Done.]

Re: How to optimize the speed of a script as much as possible.

Posted: 16 Feb 2022, 01:55
by juxwillx
You're a saviour. Thanks man!

Re: How to optimize the speed of a script as much as possible.

Posted: 16 Aug 2022, 19:49
by WAZAAAAA
Fidanza wrote:
25 Dec 2021, 14:33
WAZAAAAA wrote:
14 Feb 2015, 14:00

DllCall("Sleep",UInt,17) ;I just used the precise sleep function to wait exactly 17 milliseconds[/code]
hey the precision of this script has gone after windows 10 1803.. :!: :problem: i mean with windows 1809 and later windows 10 now uses QueryPerformanceFrequency as a fixed 10MHZ value and because of this the precision and speed of this command vanished..and i really dont understand why nobody aware of this problem.. :roll: the same problem happening with steelseries keyboard macros..macros work but slower than before on windows 10 1809 and later.. anybody know this or is there a work around to fix this ? thnx
cleaned and fixed the first post here and there
but the most important change is the high precision Sleep code thoroughly explained in point 9. which involves changing the global Windows Resolution Timer... that should fix your Sleep timing issues

Re: How to optimize the speed of a script as much as possible.

Posted: 01 Aug 2023, 15:53
by Xendrus
SetBatchLines, -1 Causes my games to completely freeze to 0 fps until the key is released.

Re: How to optimize the speed of a script as much as possible.

Posted: 02 Aug 2023, 06:18
by Animan8000
Xendrus wrote:
01 Aug 2023, 15:53
SetBatchLines, -1 Causes my games to completely freeze to 0 fps until the key is released.
That has nothing to do with AHK. Some games don't handle massive inputs at once, resulting into a freeze for some of them. Same story with Chromium and I think Electron based applications, if you spam too fast.