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

Helpful script writing tricks and HowTo's
serg
Posts: 22
Joined: 21 Mar 2015, 05:33

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

11 Jan 2017, 07:26

nnnik wrote:
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.


Seems to me, image search would be more simple solution. Just use smallest common denominator image (that is part of all search targets) + GDIP_ImageSearch func, which is exteremly fast (I use script to read text from screen - letter by letter - and it takes fraction of a second to read whole screen with GDIP_ImageSearch). This way no need to analyze shapes.
Although Im not sure what you have in mind with shape search algo, maybe it would work better in a given case
guest3456
Posts: 1859
Joined: 09 Oct 2013, 10:31

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

11 Feb 2017, 02:35

Sam_ wrote: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
---------------------------


i'm surprised that = performs that much better than := for basic integer assignment.

i am seeing similar results: ~twice as fast.

interesting.

guest3456
Posts: 1859
Joined: 09 Oct 2013, 10:31

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

11 Feb 2017, 02:53

WAZAAAAA wrote: According to the documentation (this text is found in the setup file), the Unicode x64bit version of AHK is faster, use it when available.
Spoiler


do we know WHY the Unicode x64 is faster?

guest3456
Posts: 1859
Joined: 09 Oct 2013, 10:31

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

12 Feb 2017, 23:33

Case Sensitive comparisons:

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



Results:

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

Last edited by guest3456 on 14 Feb 2017, 09:14, edited 1 time in total.

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

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

14 Feb 2017, 05:46

That's interesting. I also added to the test comparison without quotes:

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

if var = Hello World

It gave result about ~10% faster than the (case sensitive "if ==")-method - the fastest in your code above
Last edited by serg on 14 Feb 2017, 10:21, edited 1 time in total.
guest3456
Posts: 1859
Joined: 09 Oct 2013, 10:31

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

14 Feb 2017, 09:15

serg wrote:That's interesting. I also added to the test comparison without quotes:

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

if var = Hello World

It gave result about ~10% faster than the (case sensitive "if ==")-method - the fastest in your code above


Thanks, but be careful, because Traditional if = also is affected by StringCaseSense. I didn't get the same results as you. I have updated my last post. Can you delete my quote from within your post?

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

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

14 Feb 2017, 10:32

Guest3456, I ran your updated code, it gave me different results, traditional var assignments (without quotes) are the fastest :
4.884133
5.322768
4.117642
6.175888
3.612573
5.098961
So I guess PC setup makes difference here. I have Win8.1 64 with AHK 1.1.24
guest3456
Posts: 1859
Joined: 09 Oct 2013, 10:31

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

14 Feb 2017, 12:22

serg wrote:Guest3456, I ran your updated code, it gave me different results, traditional var assignments (without quotes) are the fastest :
4.884133
5.322768
4.117642
6.175888
3.612573
5.098961
So I guess PC setup makes difference here. I have Win8.1 64 with AHK 1.1.24


yep good to know. overall your numbers are significantly higher than mine

I'm on Win 10 x64, with AHK 1.1.24 x86, 3yo Core i7 laptop

i wonder if AHK x64 would change it

User avatar
jeeswg
Posts: 622
Joined: 19 Dec 2016, 01:58
Location: UK

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

15 Feb 2017, 22:00

my tests are: AHK U32 v1.1.24.05, high priority, Windows 7 x64

speed tests for generating repeated string:
'VarSetCapacity fill bytes, replace' beats 'loop append'
(repeated strings themselves are often useful for speed tests)

speed tests for parsing CRLF-delimited lines:
'parse LF ignore CR' just beats 'replace CRLF with LF, parse LF'
(out of interest 'replace CR with LF, parse LF, ignore blank lines',
also included, was essentially twice the loops, twice the time)

speed tests for InStr case sensitive/case insensitive:
case insensitive beats case sensitive
(although I might have thought case sensitive had less to check
e.g. check 'b' versus check 'B' and 'b', so would be faster)

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



==================================================

Comments on list comparison:

For a spellchecker or looking a up word in a list:
if there are 100 words or fewer, I use InStr, e.g.
if !InStr(vSpellListText, "`n" A_LoopField "`n")

For longer lists, if order doesn't matter:
I sort the needles list, (the haystack list is already sorted,)
and parse the 2 lists simultaneously.

For longer lists, if order does matter:
I prepend/append numbers to the needle lists,
sort the needle/haystack lists together,
possibly using a custom filter,
parse to find matches/non-matches,
(and append needle items to a new list with their match results,)
and restore the original order to the new needles results list.

Return to “Tutorials”

Who is online

Users browsing this forum: TravisQ and 6 guests