Just wondering if there any difference in performance between the following methods of appending values to an array:
Code: Select all
loop
{
Array[A_Index] := value
}
Code: Select all
loop
{
Array.Push(value)
}
Thanks
Code: Select all
loop
{
Array[A_Index] := value
}
Code: Select all
loop
{
Array.Push(value)
}
Code: Select all
;Push seems to be slower:
;62 234
;62 171
;63 187
q:: ;benchmark tests: 'obj[k] := v' v. 'obj.Push(v)'
vNum := 100000
oArray := []
vTickCount1 := A_TickCount
Loop, % vNum
oArray[A_Index] := "abcdefghijklmnopqrstuvwxyz"
vTickCount2 := A_TickCount
;MsgBox, % oArray.Length()
oArray := ""
oArray := []
vTickCount3 := A_TickCount
Loop, % vNum
oArray.Push("abcdefghijklmnopqrstuvwxyz")
vTickCount4 := A_TickCount
;MsgBox, % oArray.Length()
oArray := ""
vOutput := (vTickCount2-vTickCount1) " " (vTickCount4-vTickCount3)
Clipboard := vOutput
MsgBox, % vOutput
return
Code: Select all
fillArrChar(char,n){
; Returns an array of length n whose elements consist of one single symbol: char, such that
; 0<=asc(char)<=255
VarSetCapacity(str,n,asc(char))
return StrSplit(StrGet(&str,n,""))
}
fillArrStr(str,n,del:="`n"){
; Allows for an arbitrary string instead of just one character.
; Returns an array of length n whose elements consist of the string str.
; Input:
; str, string to fill array with.
; n, integer, length of the array.
; del, delimiter for strsplit. Must not be substr of str.
; Con:
; Need (to specify) a delimiter.
VarSetCapacity(var,n*strlen(n+strlen(del))*(A_IsUnicode?2:1),1)
s:=StrSplit(StrReplace(StrGet(&var,n,""),chr(1),str . del,,n),del), s.pop()
return s
}
Code: Select all
q:: ;benchmark tests: 'obj[k] := v' v. 'obj.Push(v)'
vNum := 500000
oArray := []
vTickCount1 := A_TickCount
oArray.1 := "abcdefghijklmnopqrstuvwxyz"
Loop, % vNum - 1
;oArray[A_Index] := "abcdefghijklmnopqrstuvwxyz"
oArray[oArray.Length()+1] := "abcdefghijklmnopqrstuvwxyz"
vTickCount2 := A_TickCount
;MsgBox, % oArray.Length()
oArray := ""
oArray := []
vTickCount3 := A_TickCount
Loop, % vNum
oArray.Push("abcdefghijklmnopqrstuvwxyz")
vTickCount4 := A_TickCount
;MsgBox, % oArray.Length()
oArray := ""
vOutput := (vTickCount2-vTickCount1) " " (vTickCount4-vTickCount3)
Clipboard := vOutput
MsgBox, % vOutput
return
Code: Select all
;with oArray.SetCapacity(vNum): Push faster
;297 203
;265 188
;312 187
;without oArray.SetCapacity(vNum): Push slower
;358 4196
;296 4321
;265 4322
q:: ;benchmark tests: 'obj[k] := v' v. 'obj.Push(v)'
vNum := 500000
ListLines, Off
oArray := []
oArray.SetCapacity(vNum)
vTickCount1 := A_TickCount
oArray.1 := "abcdefghijklmnopqrstuvwxyz"
Loop, % vNum - 1
;oArray[A_Index] := "abcdefghijklmnopqrstuvwxyz"
oArray[oArray.Length()+1] := "abcdefghijklmnopqrstuvwxyz"
vTickCount2 := A_TickCount
;MsgBox, % oArray.Length()
oArray := ""
oArray := []
oArray.SetCapacity(vNum)
vTickCount3 := A_TickCount
Loop, % vNum
oArray.Push("abcdefghijklmnopqrstuvwxyz")
vTickCount4 := A_TickCount
;MsgBox, % oArray.Length()
oArray := ""
vOutput := (vTickCount2-vTickCount1) " " (vTickCount4-vTickCount3)
Clipboard := vOutput
MsgBox, % vOutput
return
Code: Select all
ListLines, Off
#KeyHistory 0
#NoEnv
AutoTrim, Off
Users browsing this forum: No registered users and 157 guests