Created
August 1, 2020 04:22
-
-
Save 5Mixer/1686c01a024e5854df3d197f74621232 to your computer and use it in GitHub Desktop.
Haxe (cpp) perf testing/cache miss experiment
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
package ; | |
typedef EntityTypedef = { | |
public var health:Int; | |
public var alive:Bool; | |
} | |
class EntityClass { | |
public var health:Int; | |
public var alive:Bool; | |
public function new(health:Int, alive:Bool) { | |
this.health = health; | |
this.alive = alive; | |
} | |
} | |
class Main { | |
static function testTypedefs() { | |
trace("Testing typedefs"); | |
var entities:Array<EntityTypedef> = []; | |
for (i in 0...1000000) { | |
entities.push({ health: Math.ceil(Math.random() * 5), alive: Math.round(Math.random()) == 0 }); | |
} | |
var startTime = Sys.time(); | |
var total = 0; | |
for (entity in entities) { | |
if (entity.alive) { | |
total += entity.health; | |
} | |
} | |
trace('Unsorted ${Sys.time() - startTime } $total'); | |
entities.sort(function(a, b) { | |
if (a.alive) return -1; | |
return 1; | |
}); | |
var startTime = Sys.time(); | |
total = 0; | |
for (entity in entities) { | |
if (entity.alive) { | |
total += entity.health; | |
} | |
} | |
trace('Sorted ${Sys.time() - startTime } $total'); | |
} | |
static function testClasses() { | |
trace("Testing classes"); | |
var entities:Array<EntityClass> = []; | |
for (i in 0...1000000) { | |
entities.push(new EntityClass(Math.ceil(Math.random() * 5), Math.round(Math.random()) == 0 )); | |
} | |
var startTime = Sys.time(); | |
var total = 0; | |
for (entity in entities) { | |
if (entity.alive) { | |
total += entity.health; | |
} | |
} | |
trace('Unsorted ${Sys.time() - startTime } $total'); | |
entities.sort(function(a, b) { | |
if (a.alive) return -1; | |
return 1; | |
}); | |
var startTime = Sys.time(); | |
total = 0; | |
for (entity in entities) { | |
if (entity.alive) { | |
total += entity.health; | |
} | |
} | |
trace('Sorted ${Sys.time() - startTime } $total'); | |
} | |
static function testClassesUsingVector() { | |
trace("Testing classes"); | |
var count = 1000000; | |
var entities = new haxe.ds.Vector<EntityClass>(count); | |
for (i in 0...count) { | |
entities[i] = new EntityClass(Math.ceil(Math.random() * 5), Math.round(Math.random()) == 0 ); | |
} | |
var startTime = Sys.time(); | |
var total = 0; | |
for (entity in entities) { | |
if (entity.alive) { | |
total += entity.health; | |
} | |
} | |
trace('Unsorted ${Sys.time() - startTime } $total'); | |
entities.sort(function(a, b) { | |
if (a.alive) return -1; | |
return 1; | |
}); | |
var startTime = Sys.time(); | |
total = 0; | |
for (entity in entities) { | |
if (entity.alive) { | |
total += entity.health; | |
} | |
} | |
trace('Sorted ${Sys.time() - startTime } $total'); | |
} | |
static function testBytesForFun() { | |
trace("Testing bytes"); | |
var count = 1000000; | |
var bytes = haxe.io.Bytes.alloc(count * 2); | |
for (i in 0...count) { | |
bytes.set(i*2, Math.ceil(Math.random() * 5)); | |
bytes.set(i*2+1, Math.round(Math.random()) == 0 ? 1 : 0); | |
} | |
var data = bytes.getData(); | |
var startTime = Sys.time(); | |
var total = 0; | |
for (i in 0...count) { | |
if (haxe.io.Bytes.fastGet(data, i*2+1) == 1) { | |
total += haxe.io.Bytes.fastGet(data, i*2); | |
} | |
} | |
trace('Unsorted ${Sys.time() - startTime } $total'); | |
var sortedBytes = haxe.io.Bytes.alloc(count * 2); | |
var sortIndex = 0; | |
for (i in 0...count) { | |
if (haxe.io.Bytes.fastGet(data, i*2+1) == 1) { | |
sortedBytes.set(sortIndex*2, haxe.io.Bytes.fastGet(data, i*2)); | |
sortedBytes.set(sortIndex*2+1, 1); | |
sortIndex++; | |
} | |
} | |
for (i in 0...count) { | |
if (haxe.io.Bytes.fastGet(data, i*2+1) == 0) { | |
sortedBytes.set(sortIndex*2, haxe.io.Bytes.fastGet(data, i*2)); | |
sortedBytes.set(sortIndex*2+1, 0); | |
sortIndex++; | |
} | |
} | |
var sortedData = bytes.getData(); | |
var startTime = Sys.time(); | |
var total = 0; | |
for (i in 0...count) { | |
if (haxe.io.Bytes.fastGet(sortedData, i*2+1) == 1) { | |
total += haxe.io.Bytes.fastGet(data, i*2); | |
} | |
} | |
trace('Sorted ${Sys.time() - startTime } $total'); | |
} | |
static public function main():Void { | |
testTypedefs(); | |
testClasses(); | |
testClassesUsingVector(); | |
testBytesForFun(); | |
} | |
} |
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
Main.hx:19: Testing typedefs | |
Main.hx:33: Unsorted 0.0132510662078857 1501497 | |
Main.hx:48: Sorted 0.0189480781555176 1501497 | |
Main.hx:52: Testing classes | |
Main.hx:66: Unsorted 0.00471806526184082 1498403 | |
Main.hx:80: Sorted 0.0037529468536377 1498403 | |
Main.hx:83: Testing classes | |
Main.hx:98: Unsorted 0.00744295120239258 1498350 | |
Main.hx:112: Sorted 0.00384283065795898 1498350 | |
Main.hx:115: Testing bytes | |
Main.hx:131: Unsorted 0.00370407104492188 1501895 | |
Main.hx:158: Sorted 0.00380706787109375 1501895 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment