Which is the best way to enumerate array elements?
Code: Select all
for index, element in Array
{
element
}
Code: Select all
i := 1
Loop, % Array.MaxIndex()
{
Array[i++]
}
Code: Select all
for index, element in Array
{
element
}
Code: Select all
i := 1
Loop, % Array.MaxIndex()
{
Array[i++]
}
Code: Select all
; Example: Demonstrates QueryPerformanceCounter(), which gives more precision than A_TickCount's 10ms.
#NoEnv
SetBatchLines -1
DllCall("QueryPerformanceFrequency", "Int64*", freq)
elements := Round(1.0e7)
Array := []
Array.SetCapacity(elements)
Loop % elements
Array.Push(A_Index)
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
Loop % Array.Count()
element := Array[A_Index]
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
MsgBox % "Loop Elapsed QPC time is " . (CounterAfter - CounterBefore) / freq * 1000 " ms" ; 1400ms
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
for index, element in Array
{
}
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
MsgBox % "ForEach Elapsed QPC time is " . (CounterAfter - CounterBefore) / freq * 1000 " ms" ; 400ms
Code: Select all
DllCall("QueryPerformanceFrequency", "Int64*", freq)
elements := Round(1.0e7)
Array := []
Array.SetCapacity(elements)
Loop % elements
Array.Push(A_Index)
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
Loop % Array.Count()
myElement := Array[A_Index]
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
MsgBox % "Loop Elapsed QPC time is " (CounterAfter - CounterBefore) / freq * 1000 " ms" ; 1553 ms
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
for index, element in Array
myElement := Array[index]
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
MsgBox % "ForEach Elapsed QPC time is " (CounterAfter - CounterBefore) / freq * 1000 " ms" ; 1962 ms
Interesting. I also got the result of Loop loop being faster than For loop. This was for AHK v1.
Code: Select all
Loop % Array.Count()
myElement := Array[A_Index]
Code: Select all
for index, element in Array
myElement := Array[index]
Users browsing this forum: haomingchen1998, robodesign and 252 guests