package; import de.polygonal.ds.mem.ByteMemory; import de.polygonal.ds.mem.DoubleMemory; import de.polygonal.ds.mem.MemoryManager; import flash.Lib; import flash.Memory; import flash.utils.ByteArray; import flash.Vector; class Benchmark2 { public static function main():Void { MemoryManager.allocate(10, 1); var app:Benchmark2 = new Benchmark2(); } public function new() { test(); } public function test():Void { testRawDoubleMemory(); testMemoryManagerDoubleMemory(); testVectorDouble(); testByteArray(); testArray(); } public function testRawDoubleMemory():Void { var min:Int = 100000000; for (i in 0...10) { var t:Int = Lib.getTimer(); var v:Float = 1.0; for (r in 0...10000) { for (j in 0...512) { Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); Memory.setDouble(j, 1.0); v = Memory.getDouble(j); } } min = untyped Math.min(min, Lib.getTimer() - t); } trace("raw memory read/write: " + min + "ms."); } public function testMemoryManagerDoubleMemory():Void { var min:Int = 100000000; var doubleMemory:DoubleMemory = MemoryManager.getDoubleMemory(512); for (i in 0...10) { var t:Int = Lib.getTimer(); for (r in 0...10000) { var v:Float; for (j in 0...512) { doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); doubleMemory.set(j, 1.0); v = doubleMemory.get(j); } } min = cast Math.min(min, Lib.getTimer() - t); } trace("double memory manager read/write: " + min + "ms."); } public function testVectorDouble():Void { var min:Int = 100000000; var w:Vector = new Vector(512, true); for (i in 0...10) { var t:Int = Lib.getTimer(); for (r in 0...10000) { var v:Float = 0.0; for (j in 0...512) { w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; w[j] = 1.0; v = w[j]; } } min = untyped Math.min(min, Lib.getTimer() - t); } trace("Vector read/write: " + min + "ms."); } public function testByteArray():Void { var min:Int = 100000000; var w:ByteArray = new ByteArray(); w.length = 512; for (i in 0...10) { var t:Int = Lib.getTimer(); var v:Int = 1; for (r in 0...10000) { for (j in 0...512) { w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; w[j] = v; v = w[j]; } } min = untyped Math.min(min, Lib.getTimer() - t); } trace("byte array read/write: " + min + "ms."); } public function testArray():Void { var min:Int = 100000000; var a:Array = new Array(); for (i in 0...10) { var t:Int = Lib.getTimer(); var v:Int = 1; for (r in 0...10000) { for (j in 0...512) { a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; a[j] = v; v = a[j]; } } min = untyped Math.min(min, Lib.getTimer() - t); } trace("array read/write: " + min + "ms."); } }