Created
April 2, 2019 11:17
-
-
Save Sodaware/2a4a5ae46d45d0f64a8e50af7c260623 to your computer and use it in GitHub Desktop.
Compare performance of bmx-ng's `ArrayCopy` against manual copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: