Skip to content

Instantly share code, notes, and snippets.

@Sodaware
Created April 2, 2019 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sodaware/2a4a5ae46d45d0f64a8e50af7c260623 to your computer and use it in GitHub Desktop.
Save Sodaware/2a4a5ae46d45d0f64a8e50af7c260623 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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