Dead Code Preservation :: Archived AS3 works from wonderfl.net

Faster ceil, round, floor

Faster than Math.ceil, Math.round, Math.floor
Get Adobe Flash player
by jozefchutka 20 Jan 2011
/**
 * Copyright jozefchutka ( http://wonderfl.net/user/jozefchutka )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zCZJ
 */

package
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.text.TextField;

    [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 = 500000;
            
            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 = new Date().time - d.time;
            
            
            d=new Date;
            for(n=c;n>=-c;n-=.1) mfloor(n);
            tf.appendText("Math.floor(): " + (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;
         }

    }
}