Which one is faster for arrays and Loop Rrase? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Which one is faster for arrays and Loop Rrase?

12 Apr 2019, 03:56

Hello,

For example, split a string by ".."

Code: Select all

A..B.CD..E
to

Code: Select all

A
B.CD
E

The first method is to split the string into an array and enumerate each element.

Code: Select all

Array := StrSplit(string, "..")
for index, element in Array
{
    element
}
vs
The second way is to replace all ".." in the string with "`n" and then use Loop Prase to retrieve the substring.

Code: Select all

ReplacedStr := StrReplace(string, ".." , "`n")
Loop, Prase, ReplacedStr, `n
{
    A_LoopField
}
Which is faster and takes up less system resources?
Thanks.
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Which one is faster for arrays and Loop Rrase?

12 Apr 2019, 04:02

you could do it yourself
what is your problem ?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Which one is faster for arrays and Loop Rrase?

12 Apr 2019, 04:21

tbh I'd just benchmark this one.
Recommends AHK Studio
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Which one is faster for arrays and Loop Rrase?

12 Apr 2019, 04:24

Code: Select all

i := 1000000                  ;  a million
s := A_TickCount
Loop, % i
{	
	string := "A..B.CD..E"
	Array := StrSplit(string, "..")
	for index, element in Array
		continue
}
firstResult := A_TickCount - s
s := A_TickCount
Loop, % i
{	
	string := "A..B.CD..E"
	ReplacedStr := StrReplace(string, ".." , "`n")
	Loop, Parse, ReplacedStr, `n
		continue

}
secondResult := A_TickCount - s
MsgBox % firstResult "  vs  " secondResult
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Which one is faster for arrays and Loop Rrase?  Topic is solved

12 Apr 2019, 05:01

If you speed something in v1 you need to add SetBatchLines -1
e.g.

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
SetBatchLines -1

global Init   := DllCall("QueryPerformanceFrequency", "int64*", Freq)
global Count  := 1000000


; 1st RUN =======================================================================================================================

DllCall("QueryPerformanceCounter", "int64*", S1)

loop % Count
{
	string := "A..B.CD..E"
	Array := StrSplit(string, "..")
	for index, value in Array
		continue
}

DllCall("QueryPerformanceCounter", "int64*", E1)
Time1 := ((E1 - S1) / Freq) * 1000


; 2nd RUN =======================================================================================================================

DllCall("QueryPerformanceCounter", "int64*", S2)

loop % Count
{
	string := "A..B.CD..E"
	ReplacedStr := StrReplace(string, "..", "`n")
	loop, parse, ReplacedStr, `n
		continue
}

DllCall("QueryPerformanceCounter", "int64*", E2)
Time2 := ((E2 - S2) / Freq) * 1000


; COMPARE =======================================================================================================================

MsgBox % Time1 "`n" Time2


; ===============================================================================================================================

Code: Select all

---------------------------
benchmark.ahk
---------------------------
857.831834
676.390168
---------------------------
OK   
---------------------------
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: drani, Google [Bot], montie, Nerafius and 237 guests