ASSUME ALWAYS:
The benchmark test examples work best with the following settings, and require the QPC function.
Code: Select all
#NoEnv
SetBatchLines, -1
AutoTrim, Off
ListLines, Off
;based on QPC by wolf_II
;note: this function returns milliseconds, the original function returns seconds
;Anagrams - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=19&t=34240&p=158464#p158464
QPC()
{
static vFreq, vInit := DllCall("kernel32\QueryPerformanceFrequency", Int64P,vFreq)
DllCall("kernel32\QueryPerformanceCounter", Int64P,vCount)
return (vCount / vFreq) * 1000
}
Links:
[note: I have two posts on Page 5 which are included in this thread also]
How to optimize the speed of a script as much as possible. - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=6413
How to optimize the speed of a script as much as possible. - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/9445 ... -possible/
==================================================
BENCHMARK TESTS
[STRINGS] 4 REGEXMATCH / REGEXREPLACE TESTS
[this post]
STRING / ARRAY - CHECK IF LIST CONTAINS ITEM
https://autohotkey.com/boards/viewtopic ... 29#p192029
[STRINGS] REPEAT STRING: LOOP V. VARSETCAPACITY
[STRINGS] PARSE LINES: LOOP WITH OMITTED CHARACTERS V. STRREPLACE
[STRINGS] INSTR: CASE SENSITIVE V. CASE INSENSITIVE
https://autohotkey.com/boards/viewtopic ... 69#p194669
[STRINGS] RTRIM V. STRREPLACE
[STRINGS] TRUNCATE STRING
https://autohotkey.com/boards/viewtopic ... 72#p194672
STRINGS: SUBSTR: CROP FIRST CHARACTER V. CROP LAST CHARACTER
https://autohotkey.com/boards/viewtopic ... 71#p195571
NUMBER: SWAP BYTES (e.g. 0x11223344 to 0x44332211)
https://autohotkey.com/boards/viewtopic ... 81#p194681
[CLIPBOARD] CLIPWAIT / DLLCALL - CHECK IF CLIPBOARD EMPTY
https://autohotkey.com/boards/viewtopic ... 78#p190278
[WINDOWS] WINGETPOS V. GETWINDOWRECT (BUILT-IN COMMANDS/FUNCTIONS V. DLLCALL)
[WINDOWS] WINGET ID: A (ACTIVE WINDOW) V. 'AHK_CLASS NOTEPAD'
[WINDOWS] TWO-WAY COMPATIBILITY FUNCTIONS: WINGETPOS COMMAND V. CUSTOM WINGETPOS FUNCTION
https://autohotkey.com/boards/viewtopic ... 62#p194662
[LOOPS] LOOPED CODE V. HARDCODED CODE
https://autohotkey.com/boards/viewtopic ... 66#p194666
==================================================
4 REGEXMATCH / REGEXREPLACE TESTS
Code: Select all
q:: ;RegExMatch - [abc] v. (a|b|c)
vNum := 10000000, vText := "_"
vText := StrReplace(Format("{:" vNum "}", ""), " ", vText) ;repeat string
DllCall("QueryPerformanceFrequency", Int64P,vQPF)
Loop
{
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
;RegExMatch(vText, "[abc]")
RegExMatch(vText, "(a|b|c)")
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
}
;[abc] 228.454816 250.858284 241.224296
;(a|b|c) 609.747399 631.340479 631.166855
return
Code: Select all
q:: ;RegExMatch - [^z] v. .*?
vNum := 10000000, vText := "_"
vText := StrReplace(Format("{:" vNum "}", ""), " ", vText) "z" ;repeat string
DllCall("QueryPerformanceFrequency", Int64P,vQPF)
Loop
{
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
;RegExMatch(vText, "^[^z]*z")
RegExMatch(vText, "^.*?z")
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
}
;[^z] 15.176267 31.758203 32.513851
;.*? 122.261575 143.827286 129.315580
return
;Put here requests of problems with regular expressions - Page 14 - Ask for Help - AutoHotkey Community
;https://autohotkey.com/board/topic/12375-put-here-requests-of-problems-with-regular-expressions/page-14
;Use: <([AB])\b[^>]*>(.*?)<\/\1>
;If it's a text node you can replace .*? with [^<]* for better performance.
;Put here requests of problems with regular expressions - Page 65 - Ask for Help - AutoHotkey Community
;https://autohotkey.com/board/topic/12375-put-here-requests-of-problems-with-regular-expressions/page-65
;That's because of the lazy matching of .*?. In small strings the performance hit is negligible, but in large files it obviously makes a difference. The way around it is to use a negated character class:
Code: Select all
q:: ;RegExMatch v. RegExReplace - get initial characters
vNum := 100000, vText := "_"
vText := StrReplace(Format("{:" vNum "}", ""), " ", vText) ;repeat string
DllCall("QueryPerformanceFrequency", Int64P,vQPF)
Loop
{
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
;Loop, 1000
; RegExMatch(vText, "O)^(.{" Floor(vNum/2) "})", o)
Loop, 1000
vText2 := RegExReplace(vText, "^.{" Floor(vNum/2) "}\K.*")
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
}
;RegExMatch 124.518258 126.705235 126.224989
;RegExReplace 246.634006 260.154431 252.441425
return
;Put here requests of problems with regular expressions - Page 24 - Ask for Help - AutoHotkey Community
;https://autohotkey.com/board/topic/12375-put-here-requests-of-problems-with-regular-expressions/page-24
;RegExReplace will be somewhat slower compared to regexmatch
Code: Select all
q:: ;replacing digits with nothing
;the StrReplace variants are roughly the same speed-wise
;using StrReplace multiple times is faster than using RegExReplace once
;the interest in the StrReplace variants was re. the conversion of integers
;to strings prior to doing a StrReplace
DllCall("QueryPerformanceFrequency", Int64P,vQPF)
vNum := 2000
vText := ""
VarSetCapacity(vText, vNum*10*2)
Loop, % vNum
vText .= "0123456789"
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
Loop, % vNum
{
vText2 := vText
Loop, 10
vText2 := StrReplace(vText2, Chr(47+A_Index))
}
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
;MsgBox, % StrLen(vText) " " StrLen(vText2)
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
Loop, % vNum
{
vText2 := vText
Loop, 10
vText2 := StrReplace(vText2, A_Index-1)
}
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
;MsgBox, % StrLen(vText) " " StrLen(vText2)
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
Loop, % vNum
{
vText2 := vText
Loop, 10
vText2 := StrReplace(vText2, "" (A_Index-1))
}
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
;MsgBox, % StrLen(vText) " " StrLen(vText2)
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
Loop, % vNum
vText2 := RegExReplace(vText, "\d")
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
;MsgBox, % StrLen(vText) " " StrLen(vText2)
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
Loop, % vNum
vText2 := RegExReplace(vText, "[\d]")
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
;MsgBox, % StrLen(vText) " " StrLen(vText2)
DllCall("QueryPerformanceCounter", Int64P,vQPC1)
Loop, % vNum
vText2 := RegExReplace(vText, "[0-9]")
DllCall("QueryPerformanceCounter", Int64P,vQPC2)
MsgBox, % Clipboard := (((vQPC2-vQPC1)/vQPF)*1000)
;MsgBox, % StrLen(vText) " " StrLen(vText2)
return