Skip to content

Instantly share code, notes, and snippets.

@Sodaware
Last active April 2, 2019 11:58
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/ed1151972c586a6b62facbe986de2fef to your computer and use it in GitHub Desktop.
Save Sodaware/ed1151972c586a6b62facbe986de2fef to your computer and use it in GitHub Desktop.
ObjectBag _grow test
SuperStrict
Framework brl.basic
Const ITERATIONS:Int = 10000
Const BAG_SIZE:Int = 10000
Local startTime:Int
Local totalTime:Int
' Create an object to add outside of the test as object creation
' in the loop slows down the test.
Local obj:MyObject = New MyObject
totalTime = 0
For Local i:Int = 0 To ITERATIONS
startTime = MilliSecs()
Local bag:CurrentBag = CurrentBag.Create()
For Local t:Int = 0 To BAG_SIZE
bag.add(obj)
Next
totalTime :+ (MilliSecs() - startTime)
GCCollect()
Next
Print "CurrentBag: " + totalTime
totalTime = 0
For Local i:Int = 0 To ITERATIONS
startTime = MilliSecs()
Local bag:ArrayCopyBag = ArrayCopyBag.Create()
For Local t:Int = 0 To BAG_SIZE
bag.add(obj)
Next
totalTime :+ (MilliSecs() - startTime)
GCCollect()
Next
Print "ArrayCopyBag: " + totalTime
totalTime = 0
For Local i:Int = 0 To ITERATIONS
startTime = MilliSecs()
Local bag:FastBag = FastBag.Create()
For Local t:Int = 0 To BAG_SIZE
bag.add(obj)
Next
totalTime :+ (MilliSecs() - startTime)
GCCollect()
Next
Print "FastBag: " + totalTime
Type MyObject
Field val:Int
End Type
Type CurrentBag
Field _size:Int '''< Number of items in the bag
Field _objects:Object[] '''< Bag contents
Method add(obj:Object)
' Grow the bag if required
If Self._size = Self._objects.Length Then Self._grow()
' Add the data + move to the next position
Self._objects[Self._size] = obj
Self._size:+ 1
End Method
Method _grow(newCapacity:Int = -1)
' Use default new size if none set
If newCapacity < 0 Then newCapacity = ((Self._objects.Length * 3) / 2) + 1
' Create copy of current objects
Local oldObjects:Object[] = Self._objects
' Recreate array with new size
Self._objects = New Object[newCapacity]
' Copy the old contents back
For Local pos:Int = 0 To oldObjects.Length - 1
Self._objects[pos] = oldObjects[pos]
Next
End Method
Function Create:CurrentBag(capacity:Int = 0)
Local this: CurrentBag = New CurrentBag
this._objects = New Object[capacity]
Return this
End Function
End Type
Type ArrayCopyBag
Field _size:Int '''< Number of items in the bag
Field _objects:Object[] '''< Bag contents
Method add(obj:Object)
' Grow the bag if required
If Self._size = Self._objects.Length Then Self._grow()
' Add the data + move to the next position
Self._objects[Self._size] = obj
Self._size:+ 1
End Method
Method _grow(newCapacity:Int = -1)
' Use default new size if none set
If newCapacity < 0 Then newCapacity = ((Self._objects.Length * 3) / 2) + 1
' Create copy of current objects
Local oldObjects:Object[] = Self._objects
' Recreate array with new size
Self._objects = New Object[newCapacity]
' Copy the old contents back.
ArrayCopy(oldObjects, 0, Self._objects, 0, oldObjects.Length)
End Method
Function Create: ArrayCopyBag(capacity:Int = 0)
Local this:ArrayCopyBag = New ArrayCopyBag
this._objects = New Object[capacity]
Return this
End Function
End Type
Type FastBag
Field _size:Int
Field _objects:Object[]
Method add(obj:Object)
' Grow the bag if required
If Self._size = Self._objects.Length Then Self._grow()
' Add the data + move to the next position
Self._objects[Self._size] = obj
Self._size:+ 1
End Method
Method _grow(newCapacity:Int = -1)
' Use default new size if none set
If newCapacity < 0 Then newCapacity = ((Self._objects.Length * 3) / 2) + 1
Self._objects = Self._objects[ .. newCapacity]
End Method
Function Create:FastBag(capacity:Int = 0)
Local this:FastBag = New FastBag
this._objects = New Object[capacity]
Return this
End Function
End Type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment