Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare performance of bmx-ng's `ArrayCopy` against manual copy
Framework brl.basic
Const ITERATIONS:Int = 1000000
Const ARRAY_SIZE:Int = 1000
Local startTime:Int
' ------------------------------------------------------------------------------
' -- Copying arrays of Int
' Create a source array and fill with random data.
Local sourceArray:Int[ARRAY_SIZE]
For Local i:Int = 0 To sourceArray.length - 1
sourceArray[i] = Rand(0, ARRAY_SIZE)
Next
' Test copying using `ForEach`
startTime = MilliSecs()
For Local i:Int = 1 To ITERATIONS
Local destArray:Int[sourceArray.Length]
For Local t:Int = 0 To sourceArray.Length - 1
destArray[t] = sourceArray[t]
Next
Next
Print "Copy (foreach): " + (MilliSecs() - startTime)
GCCollect()
' Test copying using `ArrayCopy`
startTime = MilliSecs()
For Local i:Int = 1 To ITERATIONS
Local destArray:Int[sourceArray.length]
ArrayCopy(sourceArray, 0, destArray, 0, sourceArray.Length)
Next
Print "ArrayCopy: " + (MilliSecs() - startTime)
' ------------------------------------------------------------------------------
' -- Copying arrays of Object
' Create a source array and fill with random data.
Local objectArray:MyType[ARRAY_SIZE]
For Local i:Int = 0 To objectArray.length - 1
objectArray[i] = MyType.Create(Rand(0, ARRAY_SIZE))
Next
GCCollect()
' Test copying using `ForEach`
startTime = MilliSecs()
For Local i:Int = 1 To ITERATIONS
Local destArray:MyType[objectArray.Length]
For Local t:Int = 0 To objectArray.Length - 1
destArray[t] = objectArray[t]
Next
Next
Print "Object Copy (foreach): " + (MilliSecs() - startTime)
GCCollect()
' Test copying using `ArrayCopy`
startTime = MilliSecs()
For Local i:Int = 1 To ITERATIONS
Local destArray:MyType[objectArray.length]
ArrayCopy(objectArray, 0, destArray, 0, objectArray.Length)
Next
Print "Object ArrayCopy: " + (MilliSecs() - startTime)
Type MyType
Field value:Int
Function Create:MyType(val:Int)
Local this:MyType = New MyType
this.value = val
Return this
End Function
End Type
@Sodaware

This comment has been minimized.

Copy link
Owner Author

@Sodaware Sodaware commented Apr 2, 2019

Sample output:

Copy (foreach): 2249
ArrayCopy: 1127
Object Copy (foreach): 3264
Object ArrayCopy: 2152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment