Faster Math with Bitwise Shift
/**
* Copyright hidrodixtion2 ( http://wonderfl.net/user/hidrodixtion2 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wXFx
*/
// forked from jozefchutka's Faster ceil, round, floor
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.utils.getTimer;
[SWF(width="465", height="465", frameRate="30", backgroundColor="#FFFFFF")]
public class WonderflApp extends Sprite
{
public function WonderflApp():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var tf:TextField = new TextField();
tf.width = stage.stageWidth;
tf.height = stage.stageHeight;
tf.text = "";
tf.wordWrap = true;
tf.multiline = true;
addChild(tf);
var n:Number, d:Date;
var c:Number = 100000000;
tf.appendText("TESTS:");
for(n=20;n>=-20;n-=1)
tf.appendText(Math.floor(n/10) == floor(n/10) ? "1 " : "0 ");
for(n=20;n>=-20;n-=1)
tf.appendText(Math.round(n/10) == round(n/10) ? "1 " : "0 ");
for(n=20;n>=-20;n-=1)
tf.appendText(Math.ceil(n/10) == ceil(n/10) ? "1 " : "0 ");
tf.appendText("... FINISHED.\n");
d=new Date;
for(n=c;n>=-c;n-=.1) emptyf(n);
var e:Number = 0;//new Date().time - d.time;
d=new Date;
//for(n=c;n>=-c;n-=.1) mfloor(n);
for (n=0; n<=c; n++) {
n = n * 2;
}
tf.appendText("Math.floor(): " + getTimer());//+ (new Date().time - d.time - e) + " ms\n");
d=new Date;
for(n=c;n>=-c;n-=.1) floor(n);
tf.appendText("floor(): " + (new Date().time - d.time - e) + " ms\n");
d=new Date;
for(n=c;n>=-c;n-=.1) mround(n);
tf.appendText("Math.round(): " + (new Date().time - d.time - e) + " ms\n");
d=new Date;
for(n=c;n>=-c;n-=.1) round(n);
tf.appendText("round(): " + (new Date().time - d.time - e) + " ms\n");
d=new Date;
for(n=c;n>=-c;n-=.1) mceil(n);
tf.appendText("Math.ceil(): " + (new Date().time - d.time - e) + " ms\n");
d=new Date;
for(n=c;n>=-c;n-=.1) ceil(n);
tf.appendText("ceil(): " + (new Date().time - d.time - e) + " ms\n");
}
private function emptyf(n:Number):Number{return n;}
private function mfloor(n:Number):Number{return Math.floor(n);}
private function mround(n:Number):Number{return Math.round(n);}
private function mceil(n:Number):Number{return Math.ceil(n);}
private function floor(n:Number):int
{
var ni:int = n;
return (n < 0 && n != ni) ? ni - 1 : ni;
}
private function round(n:Number):int
{
return n < 0 ? n + .5 == (n | 0) ? n : n - .5 : n + .5;
}
private function ceil(n:Number):int
{
var ni:int = n;
return (n >= 0 && n != ni) ? ni + 1 : ni;
}
}
}