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

Helpful script writing tricks and HowTo's
User avatar
jNizM
Posts: 2120
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

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

27 Oct 2016, 07:29

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

if (IsTrue = 1):	9.594683
if (IsTrue): 7.755995 ; <- fastes

if (IsFalse = 0): 9.508504
if not (IsFalse): 8.588009
if (IsFalse <> 1): 9.613642
if !(IsFalse): 8.520527 ; <- fastes


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

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

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

27 Oct 2016, 12:16

nice work.

if (IsTrue): 7.755995 ; <- fastes
We got the same results here

if !(IsFalse): 8.520527 ; <- fastes
I dont think i did testing with ! but i'm unsure

if not (IsFalse): 8.588009
hmm i got this to be slower than <>1

maybe an achitecture difference ?
Gonna check up in next time i try to optimize some code


I have a question abotu optimize arithmics.
i was under the assumption that + is faster than - and that Paranthes would hurt performance
so i tried to optimize this:

test := ((newKrnlTime-oldKrnlTime) + (newUserTime-oldUserTime)) // (newTickCount - oldTickCount) // 800

to
test := ((newKrnlTime+newUserTime - (oldKrnlTime+oldUserTime)) // (newTickCount - oldTickCount) // 800

Which made one - into a + and removed a parenthesis pair bit it came out slightly slower

can i assumed multiplications are faster than interger division ?
would it help to change

// (newTickCount - oldTickCount) // 800

to

// ((newTickCount - oldTickCount) * 800)


I've always though the order of cost in operators would be
+ Fastest
-
*
/ slowest (Why we try to use bit sift operations instead)
User avatar
jNizM
Posts: 2120
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

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

28 Oct 2016, 01:00

100.000.000 Loops

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

A + B   7.822086
A - B 7.793425

A + 1 7.647965
A - 1 7.629782

A++ 6.906725
A-- 6.837371

++A 7.220017
--A 7.273083
[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
User avatar
jNizM
Posts: 2120
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

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

28 Oct 2016, 01:23

Calc with 'power of 2' numbers


262144 / 1024 -> 256 (100.000.000 Loops)

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

┌──────────────────┬──────────────┬────────────┐
│ │ Return │ Time │
├──────────────────┼──────────────┼────────────┤
│ A / 1024256.0000007.596106
Floor(A / 1024)2569.746309
Round(A / 1024)2569.663673
│ A // 10242568.199030
│ A >> 102567.652976
└──────────────────┴──────────────┴────────────┘



256 * 1024 -> 262144 (100.000.000 Loops)

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

┌──────────────────┬──────────────┬────────────┐
│ │ Return │ Time │
├──────────────────┼──────────────┼────────────┤
│ B * 10242621447.626448
│ B << 102621447.685819
└──────────────────┴──────────────┴────────────┘



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

[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
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

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

28 Oct 2016, 18:10

Nice thread :thumbup:

I have a couple of ideas too I would like to share, it's probably obvious.
I use a huge program running at external desktop, and the only ways to get information from the program is by reading colors (imagesearch, pixelgetcolor and pixelsearch), reading window title or window size.
Because I've made a 6k lines script to maximize effeciency, I've learn that these tips may reduce noticeable lags (when waiting for imagesearch to finnish).

1. Multiple boolean IF/OR/AND
Situation 1 - There is some functions - checkSlow(), checkFast() and checkFastest(). Those name just gives a hint about the time it takes for those functions to return a result. If testing if all of those gives true, the most efficient way is normally by putting the fastest function first, bedause if that one return 0, a multiple AND statement won't waste time calling the next function.

Good idea:
If ( checkFastest() && checkFast() && checkSlow() )
do something


If there is a multiple boolean AND operator (if I can call it so?) and each function call takes about the same time, it's a good idea to put first the function that is most likely to return 0. For multiple OR operators, inverse rules applies.


2. Multiple imagesearch
ImageSearch will be relatively slow when searching a large area of screen. If the image to match is likely to stay in same location between multiple imagesearch calls, it is a good idea to make the second imagesearch (after the first imagesearch where a match was found) to only search within the matching area that measure the same dimensions as the image file.


Also I recently found a very useful resource - PixelChecksum()
https://autohotkey.com/boards/viewtopic.php?f=6&t=4431
That seems faster than using ImageSearch. Too bad it doesn't work in 64 bit WIndows.
Sam_
Posts: 31
Joined: 20 Mar 2014, 20:24

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

22 Nov 2016, 14:39

.SetCapacity is the Array/Object equivalent of VarSetCapacity, and can significantly improve performance.

Test Script:

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



Results:
---------------------------
benchmarks.ahk
---------------------------
NOT using SetCapacity: 375.618883

using SetCapacity: 147.962063
---------------------------
OK
---------------------------
Sam_
Posts: 31
Joined: 20 Mar 2014, 20:24

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

22 Nov 2016, 15:24

Some testing of different ways to assign key, value pairs in arrays:

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



Results:

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

---------------------------
benchmarks.ahk
---------------------------
Array.Push() 14.231856
ObjRawSet() 13.684982
Array[]:= 12.596166 ; <- fastes
Array.Insert() 16.269619
Array.InsertAt() 26.067634
---------------------------
OK
---------------------------
Sam_
Posts: 31
Joined: 20 Mar 2014, 20:24

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

29 Nov 2016, 12:08

@V for Vendetta, Here are my results:
Direct value assignment:

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



Result:

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

---------------------------
benchmarks.ahk
---------------------------
Var=1 21.113292 ; <- faster
Var:=1 56.657990
---------------------------
OK
---------------------------



String Concatenation:

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



Results:

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

---------------------------
benchmarks.ahk
---------------------------
173.310786
72.365218
69.749422 ; <- fastest
---------------------------
OK
---------------------------

This would indicate that using the ".=" operator in conjunction with (when applicable) the explicit "." concatenation operator is fastest. That last bit surprises me... See Operators.
Further testing indicates this is false. See my next post.
Last edited by Sam_ on 30 Nov 2016, 10:47, edited 3 times in total.
User avatar
V for Vendetta
Posts: 105
Joined: 29 Sep 2016, 11:33

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

29 Nov 2016, 13:23

Sam_ wrote:This would indicate that using the ".=" operator in conjunction with (when applicable) the explicit "." concatenation operator is fastest. That last bit surprises me... See Operators.


From the tests I made, both seem to be the same!

loop, 1000000
text .= a_index "`r`n"

loop, 1000000
text .= a_index . "`r`n"

Result from my pc (for loop 1 000 000): both varies from 10 000 to 12 000 milliseconds!!!
FanaticGuru
Posts: 611
Joined: 30 Sep 2013, 22:25

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

30 Nov 2016, 00:30

Sam_ wrote:Results:

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

---------------------------
benchmarks.ahk
---------------------------
173.310786
72.365218
69.749422 ; <- fastest
---------------------------
OK
---------------------------

This would indicate that using the ".=" operator in conjunction with (when applicable) the explicit "." concatenation operator is fastest. That last bit surprises me... See Operators.

My results did not concur. The last two on my system were very similar in multiple test. Less than 1% deviation plus or minus with neither consistently faster.

I did add SetBatchLines -1 and Process, Priority, , H to my script to try to limit other processes on my computer stealing processor time.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
Sam_
Posts: 31
Joined: 20 Mar 2014, 20:24

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

30 Nov 2016, 10:42

@V for Vendetta, @FanaticGuru
After further testing, I have arrived at the same conclusion as you. I set the following script up and let it run for a few hours last night

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



After 4,011 loops (of ten million concatenations using each method), no explicit "." syntax resulted in an average of 2.037523 sec. while with it had an average of 2.037380 sec. I imagine that's well within the margin of error of the test. In terms of speed, concatenation with or without the "." operator is identical.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

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

17 Dec 2016, 19:30

Hi. I'm not where I talk so much about those tiny micro seconds one operator compares to another. Instead I focus on how to improve the kind of taksk that I know takes times, and how to make it as effective as possible.

Todays examle - Say I have 4 function, each normally takes noticeable time to complete. It might be a imagesearch for each function, so if possible - the goal is to run as few as possible function calls (one function return true if it had success).
Most of explanation is to be found in comments.

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

Helgef
Posts: 1646
Joined: 17 Jul 2016, 01:02

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

21 Dec 2016, 14:12

Almost_there wrote:Hi. I'm not where I talk so much about those tiny micro seconds one operator compares to another. Instead I focus on how to improve the kind of taksk that I know takes times, and how to make it as effective as possible.

A sound approach! :thumbup:
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

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

04 Jan 2017, 18:55

Next version is done. Now I have added a counter and a bubble-sort (slightly modified) routine so that the function that returns true the most number of times, is the function that is called first.

Most of explanation is done by comments - read well, folks :D

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

serg
Posts: 35
Joined: 21 Mar 2015, 05:33

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

09 Jan 2017, 05:33

This is quite useful thread, I want to add few points:

A) Regular vars are retrieved faster than array elements (in example below ~3 times faster):

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




B) "Loop, Parse" is faster than "StringSplit" - as mentioned in AHK documentation

C) To convert float to integer, Round() is ~30-35% faster than [SetFormat Float 0.0 + then set back to original]

D) Vars with many references in names (i.e. var%a%_%b%) reduce speed quite significantly (noticable in Loops)

E) Not sure about this one, but according to my testing:
when working with large data sets and need to retrieve parts of the data fast, the fastest ways are:
1) create structured file with File.Write where each field is fixed size 4 or 8... bits.
Then retrieve data using File.Read
or
2) "FileRead + InStr()/SubStr()" - in cases where possible to use SubStr() instead of Loop,Parse
These 2 methods are much faster than "Loop,Read" or "FileRead + Loop,Parse"
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

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

10 Jan 2017, 20:12

moved to separate thread as advised

https://autohotkey.com/boards/viewtopic ... 42#p125442
Last edited by Almost_there on 11 Jan 2017, 11:29, edited 2 times in total.
User avatar
nnnik
Posts: 1988
Joined: 30 Sep 2013, 01:01
Location: Germany

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

11 Jan 2017, 03:46

If you want more speed you need to change how you recognize buttons (basically stop using ImageSearch).
Also the way you wrote that script is pretty cancerous.
Rather than defining one function which let's you search for different Images at different positions you define like 8 functions and even worse rather than storing the results somewhere you let each function have them store it in static parameters.
Recommends AHK Studio
serg
Posts: 35
Joined: 21 Mar 2015, 05:33

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

11 Jan 2017, 04:19

@Almost_there
"Gdip_ImageSearch" by MasterFocus is much faster than native "ImageSearch", - at least it used to be. I use it for reading text from screen.
Althou your last post should probably be in a separate thread
User avatar
nnnik
Posts: 1988
Joined: 30 Sep 2013, 01:01
Location: Germany

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

11 Jan 2017, 06:10

serg wrote:@Almost_there
"Gdip_ImageSearch" by MasterFocus is much faster than native "ImageSearch", - at least it used to be. I use it for reading text from screen.
Althou your last post should probably be in a separate thread
I actually meant something completely different from ImageSearch.
Maybe a Shape search algorythm.
Recommends AHK Studio

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 3 guests