Skip to content

Instantly share code, notes, and snippets.

@Dr-Emann
Created March 13, 2012 06:25
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 Dr-Emann/2027229 to your computer and use it in GitHub Desktop.
Save Dr-Emann/2027229 to your computer and use it in GitHub Desktop.
Testing Haxe Rounding Speeds
package net.zdremann.tmp.roundto;
import de.polygonal.core.fmt.Sprintf;
import de.polygonal.core.math.Limits;
import de.polygonal.core.math.Mathematics;
import haxe.Timer;
import nme.display.Sprite;
import nme.events.Event;
import nme.Lib;
#if !js
import nme.Memory;
#end
import nme.text.TextField;
import nme.utils.ByteArray;
/**
* ...
* @author Zachary Dremann
*/
class Main extends Sprite
{
static public inline var ITERATIONS:Int =
#if flash
1000000;
#elseif neko
100000;
#elseif cpp
2000000;
#else
500000;
#end
static public function main()
{
Lib.current.addChild(new Main());
}
var randomField:TextField;
var stdField:TextField;
var myMethodField:TextField;
var mathematicsField:TextField;
var mixedField:TextField;
public function new()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.frameNum = 0;
this.randomAve = 0;
this.stdAve = 0;
this.myMethodAve = 0;
this.mathematicsAve = 0;
this.mixedAve = 0;
this.randomField = new TextField();
this.stdField = new TextField();
this.myMethodField = new TextField();
this.mathematicsField = new TextField();
this.mixedField = new TextField();
this.randomField.x = this.stdField.x = this.myMethodField.x = this.mathematicsField.x = this.mixedField.x = 50;
this.randomField.width = stdField.width = myMethodField.width = mathematicsField.width = mixedField.width = 500;
this.randomField.y = 50;
this.stdField.y = 100;
this.myMethodField.y = 150;
this.mathematicsField.y = 200;
this.mixedField.y = 250;
this.addChild(randomField);
this.addChild(stdField);
this.addChild(myMethodField);
this.addChild(mathematicsField);
this.addChild(mixedField);
#if js
randomNums = new Array();
#else
var mem = new ByteArray();
#if flash
mem.length = ITERATIONS * 4;
#else
mem.setLength(ITERATIONS * 4);
#end
trace(Mathematics.round(5.241));
Memory.select(mem);
#end
}
#if js
var randomNums:Array<Float>;
#end
private function onAddedToStage(e:Event):Void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.addEventListener(Event.ENTER_FRAME, runTests);
}
var frameNum:Int;
var randomAve:Float;
var stdAve:Float;
var myMethodAve:Float;
var mathematicsAve:Float;
var mixedAve:Float;
private function runTests(?e:Event = null):Void
{
var NUM_OF_TESTS = 5;
var previousIterations = Math.floor(frameNum / NUM_OF_TESTS);
var timeElapsed = 0;
var randomNum = 0.0;
var roundedNum = 0.0;
switch(frameNum % NUM_OF_TESTS)
{
case 0:
// Random Number Generation Only
var t1 = Lib.getTimer();
var arrFloats:Array<Float> = new Array();
for (i in 0...ITERATIONS)
{
#if js
randomNums[i] = Math.random() * (Limits.INT32_MAX * 3.0) - Limits.INT32_MAX * 1.5;
#else
Memory.setFloat(i * 4, Math.random() * (Limits.INT32_MAX * 3.0) - Limits.INT32_MAX * 1.5);
#end
}
timeElapsed = Lib.getTimer() - t1;
randomAve = (randomAve * previousIterations + timeElapsed) / (previousIterations+1);
case 1:
// My Method
var t1 = Lib.getTimer();
var t:Float;
for (i in 0...ITERATIONS)
{
randomNum =
#if js
randomNums[i];
#else
Memory.getFloat(i * 4);
#end
t = randomNum / 0.1;
t = (t > 0?t + .5:(t < 0?t - .5:t));
roundedNum = (t - t % 1) * 0.1;
}
timeElapsed = Lib.getTimer() - t1;
myMethodAve = (myMethodAve * previousIterations + timeElapsed) / (previousIterations+1);
case 2:
// Std Method
var t1 = Lib.getTimer();
for (i in 0...ITERATIONS)
{
randomNum =
#if js
randomNums[i];
#else
Memory.getFloat(i * 4);
#end
roundedNum = Math.round(randomNum / 0.1) * 0.1;
}
timeElapsed = Lib.getTimer() - t1;
stdAve = (stdAve * previousIterations + timeElapsed) / (previousIterations+1);
case 3:
// Mathematics Method
var t1 = Lib.getTimer();
for (i in 0...ITERATIONS)
{
randomNum =
#if js
randomNums[i];
#else
Memory.getFloat(i * 4);
#end
roundedNum = Mathematics.round(randomNum / 0.1) * 0.1;
}
timeElapsed = Lib.getTimer() - t1;
mathematicsAve = (mathematicsAve * previousIterations + timeElapsed) / (previousIterations+1);
case 4:
// Mixed Method
var t1 = Lib.getTimer();
for (i in 0...ITERATIONS)
{
randomNum =
#if js
randomNums[i];
#else
Memory.getFloat(i * 4);
#end
var t = randomNum / 0.1;
if (t < Limits.INT32_MAX && t > Limits.INT32_MIN)
{
roundedNum = Mathematics.round(t) * .1;
}
else
{
t = (t > 0?t + .5:(t < 0?t - .5:t));
roundedNum = (t - t % 1) * 0.1;
}
}
timeElapsed = Lib.getTimer() - t1;
mixedAve = (mixedAve * previousIterations + timeElapsed) / (previousIterations+1);
}
myMethodField.text = Sprintf.format("My Method Average: %.3f", [myMethodAve]);
stdField.text = Sprintf.format("Std Method Average: %.3f", [stdAve]);
randomField.text = Sprintf.format("Random Generation Average: %.3f", [randomAve]);
mathematicsField.text = Sprintf.format("Mathematics method average: %.3f", [mathematicsAve]);
mixedField.text = Sprintf.format("Mixed Method Average: %.3f", [mixedAve]);
frameNum++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment