In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

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

forked from: 関数のオーバーヘッドありすぎない?

/**
 * Copyright beinteractive ( http://wonderfl.net/user/beinteractive )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zDaO
 */

// forked from keno42's 関数のオーバーヘッドありすぎない?
package {
    import flash.display.Sprite;
    import flash.text.*;
    import flash.utils.*;
    public class FlashTest extends Sprite {
        private static const COUNT:uint = 2000000;
        private var tf:TextField = new TextField();
        public function FlashTest() {
            // write as3 code here..
            addChild(tf);
            tf.autoSize = "left";
            testInline();
            testPrivate();
            testStatic();
            testClosure();
            
        }
        private function privateFunc(value:Number):Number{
            return (value + value * value) / value;
        }
        private static function staticFunc(value:Number):Number{
            return (value + value * value) / value;
        }
        
        private function testInline():void
        {
            var time:uint = getTimer();
            var i:Number, j:Number;
            for( i=0; i < COUNT; i++ ){
                j = (i + i * i) / i;
            }
            tf.appendText("インライン: " + String(getTimer()-time) + "\n");
        }
        private function testPrivate():void
        {
            var time:uint = getTimer();
            var i:Number, j:Number;
            for( i=0; i < COUNT; i++ ){
                j = privateFunc(i);
            }
            tf.appendText("private関数: " + String(getTimer()-time) + "\n");
        }
        private function testStatic():void
        {
            var time:uint = getTimer();
            var i:Number, j:Number;
            for( i=0; i < COUNT; i++ ){
                j = staticFunc(i);
            }
            tf.appendText("static関数: " + String(getTimer()-time) + "\n");
        }
        private function testClosure():void
        {
            var func:Function = function(value:Number):Number{
                return (value + value * value) / value;
            }
            
            var time:uint = getTimer();
            var i:Number, j:Number;
            for( i=0; i < COUNT; i++ ){
                j = func(i);
            }
            tf.appendText("関数: " + String(Number(getTimer())-time) + "\n");
        }
    }
}