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

Helpful script writing tricks and HowTo's
dsewq1LYJ
Posts: 102
Joined: 26 Aug 2014, 23:21

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

24 Sep 2015, 08:13

WAZAAAAA wrote:
dsewq1LYJ wrote:Higher precision but higher usage ?
And may I ask.
What's the lowest delay of the function
According to my benchmarks:
Sleep default method = low CPU, low precision
Sleep suspend method = low CPU, high precision, issues with SetTimer
Sleep precise method = high CPU, higher precision
But sometimes when using the precise method, it eats so much CPU that it actually ends up slowing everything down, this has happened to a friend of mine too with a weaker PC, that's why I have replaced it with the suspend mode.
You can read more about precise sleeps at RHCP's thread: http://www.autohotkey.com/board/topic/9 ... -function/

I don't understand what do you mean with lowest delay.

May I ask.What's the situation when Ternary faster than Genreral If condition ?
ozzii
Posts: 279
Joined: 30 Oct 2013, 06:04

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

25 Sep 2015, 02:07

I don't know why it's faster (or if it is), it's not me who gave that statement.

But the number of parameters don't do things slower, at least I think so.
User avatar
jNizM
Posts: 2154
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

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

25 Sep 2015, 03:26

Ternary vs if/else (Short)

Code: [Select all] [Download] GeSHi © Codebox Plus

Ternarry:        2.828439
if/else: 3.931492

Code:
Spoiler



Ternary vs if/else (Big)

Code: [Select all] [Download] GeSHi © Codebox Plus

Ternarry:        tba...
if/else: tba...

Spoiler
[AHK] 1.1.25.02 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.1198) x64
My GitHub Profile | Donations are appreciated if I could help you
dsewq1LYJ
Posts: 102
Joined: 26 Aug 2014, 23:21

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

20 Oct 2015, 08:16

jNizM wrote:Ternary vs if/else (Short)

Code: [Select all] [Download] GeSHi © Codebox Plus

Ternarry:        2.828439
if/else: 3.931492

Code:
Spoiler



Ternary vs if/else (Big)

Code: [Select all] [Download] GeSHi © Codebox Plus

Ternarry:        tba...
if/else: tba...

Spoiler

Can not wait anymore for the final result :D

Thank you so much!
dsewq1LYJ
Posts: 102
Joined: 26 Aug 2014, 23:21

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

21 Oct 2015, 04:03

Can someone test follow code for me...

I got bad PC and I am a student, I cant use Computer all day (even just place it and wait for it to finish)

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



-------------------
Edit the title to whatever you want.

Any help I'll appreciate it :D
just me
Posts: 4355
Joined: 02 Oct 2013, 08:51
Location: Germany

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

21 Oct 2015, 06:58

You might want to look at SetWinDelay.
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
GitHub: HinkerLoden

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

26 Mar 2016, 06:27

*bump

I am prepare atm my post in the forum - its a bit complitcated. But i dont wanna post wrong things.

So we have the general performance settings, which can use in most cases without troubleshooting

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus





All possible Performance Parameters with short Description

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

SvenBent
Posts: 248
Joined: 09 Aug 2015, 01:34

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

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.
SvenBent
Posts: 248
Joined: 09 Aug 2015, 01:34

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

26 Mar 2016, 15:29

a little tip

Avoid spreading math over multiple lines and using variable for intermediate results if they are only used once.
As much as possible condense math into one line and only use variables for storing math results if you need the results being used multiple times later.

remember that:
1x calc < 1x calc + 1x memory read < 2x calc



Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus



The second condensed method is slightly more than 50% faster
Last edited by SvenBent on 28 Mar 2016, 00:05, edited 1 time in total.
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
GitHub: HinkerLoden

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

26 Mar 2016, 17:16

do a function and the lines in it count as one operation or count the lines inside the function for the memory handling. Those things really make sense in script with a high calculating processes.

I have more the problem to reduce now the Speed from a script , because i get stutters. I integrate this inside the help post, what is atm in under construction. (FastimageSearch is fast but have some little problems)
SvenBent
Posts: 248
Joined: 09 Aug 2015, 01:34

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

27 Mar 2016, 01:31

HinkerLoden wrote:do a function and the lines in it count as one operation or count the lines inside the function for the memory handling. Those things really make sense in script with a high calculating processes.

I have more the problem to reduce now the Speed from a script , because i get stutters. I integrate this inside the help post, what is atm in under construction. (FastimageSearch is fast but have some little problems)


I'm sorry I'm not getting what you are trying to say. Is the first line a question or a statement ?
If your AHK script is making other software stutter, then Tty to reduce the priority to BelowNormal. Having CPU heavy stuff run in High or Realtime is a perfect way to make a computer unresponsive.
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
GitHub: HinkerLoden

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

27 Mar 2016, 04:14

a function count as one operation process or each line inside the function ?
User avatar
boiler
Posts: 1678
Joined: 21 Dec 2014, 02:44

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

27 Mar 2016, 13:32

If you are asking if calling a function only takes the processing time of one typical line of code rather than the time it takes to execute all of the lines inside the function, the answer is that it takes the time to execute all of the lines of code inside the function. There's nothing magical about putting lines of code inside a function that makes them free. It still takes CPU time to do stuff.
User avatar
SnowFlake
Posts: 363
Joined: 28 Apr 2015, 05:41
Google: floowsnaake
GitHub: floowsnaake
Contact:

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

01 Apr 2016, 13:51

any examples on how
PixelSearch,,,1,1,1,1,0xEEEEEE
is faster?
Contact Info: https://github.com/floowsnaake // FloowSnaake(A)gmail.com // https://www.youtube.com/user/FloowSnaake IF you need Help send me a PM,Email or check if im in the IRC #ahk,#ahkscriptImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
I sense a disturbance in the force ... | (Do, or do not. There is no try.)
SvenBent
Posts: 248
Joined: 09 Aug 2015, 01:34

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

01 Apr 2016, 18:10

T His is probably way down into miniscule area but
Using Gosub seems to be 25% faster than using a function.
User avatar
WAZAAAAA
Posts: 63
Joined: 13 Jan 2015, 19:48

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

05 Apr 2016, 07:30

SnowFlake wrote:any examples on how
PixelSearch,,,1,1,1,1,0xEEEEEE
is faster?
More information on point 10 from the first post.
Otherwise try this benchmark yourself:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

PixelSearch,,,1,1,1,1,0xEEEEEE looped for 500000 times took me 3,3 seconds
PixelSearch,,,1,1,1,1,0xEEEEEE,,Fast looped for 500000 times took me 8,1 seconds
(tested with V-Sync OFF on Windows which appears to loop much much faster)
Last edited by WAZAAAAA on 31 Oct 2016, 20:39, edited 2 times in total.
YOU'RE NOT ALEXANDER
SvenBent
Posts: 248
Joined: 09 Aug 2015, 01:34

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

19 May 2016, 09:28

Another miniscule speed tip

Replacing

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

    DllCall("SetProcessWorkingSetSize", Int,-1, Int,-1, Int,-1 )

with

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

    return, dllcall("psapi.dll\EmptyWorkingSet", "UInt", -1)

You get around a 27% speedup on that emptymem call


also Doing Loop with a set value instead of a variable is 1.2% faster
Further investigation seems to deny this
SvenBent
Posts: 248
Joined: 09 Aug 2015, 01:34

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

20 May 2016, 15:38

a few tips for IF checking of Boolean values
Tested on a Core2Quad Q6600 system.


if VariableName
Seems to be the fastest way to check if a variable is True


if VariableName = 0
Is the fastest way to check if a variable is false however it does not take into account of the variable is not set, aka empty. The IF commands does not get activaged if the variable is not set/empty

if VariableName <> 1
is almost as fast and an empty variable is considere false ( aka the IF settings get activated) just like if it contained a 0

if Not VariableName
Seems to be slower than both of the two above
User avatar
rootmos
Posts: 36
Joined: 11 Mar 2016, 04:38
GitHub: rootm0s
Location: Sweden
Contact:

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

31 May 2016, 15:37

This is awesome! Thanks for sharing! :)
Look at me, I am your god now.
User avatar
WAZAAAAA
Posts: 63
Joined: 13 Jan 2015, 19:48

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

27 Oct 2016, 07:02

- added various inputs from various users (thanks to jNizM, dd900, SvenBent)
- changed Priority from H to A under SvenBent's recommendation. I used H initially because I was under the seemingly wrong impression that "Above Normal" wasn't supported in every Windows operating system
- modified benchmark script, which is now shorter and slightly more precise
- slightly changed RegEx code
- minor fixes here and there
YOU'RE NOT ALEXANDER

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests