処理速度比較:Function型の呼び出し方による違い

by 110100110101101
Function 型には apply() と call() というメソッドが入っています。
これを使って呼び出す場合、処理速度に違いが出るのか調べてみました。

結果:10倍以上遅くなります。
ですが、Function型の変数に入れて実行すると幾分早く処理できます。

Function 型
http://help.adobe.com/ja_JP/FlashPlatform/reference/actionscript/3/Function.html
♥0 | Line 280 | Modified 2012-02-22 22:22:53 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            addChild( new Test() );
        }
    }
}

    import flash.display.Sprite;
    class Test
    extends Sprite
    {
        public function test_1():void{}
        public function test_2( value:Number = 0 ):void{}
        public function test_3( argmentsArray:Array = undefined ):void
        {
            argmentsArray[0];
            argmentsArray[1];
        }        
        public function test_4( argmentsObject:Object = undefined ):void
        {
            argmentsObject.x
            argmentsObject.title
        }
        public function Test()
        {
            var note:LLLNote = new LLLNote( this );
            var i:int,iMax:int = 1000000;
            
            note.text = "count " + iMax.toString() + "\n";
            
            note.text += "------------------------------\n";
            
            note.text += "public function test_1():void{} \n";
            note.text += "public function test_2( value:Number = 0 ):void{} \n";
            note.text += "var func_1:Function = test_1; \n" ;
            note.text += "var func_2:Function = test_2; \n";
                      
            var nest:Nest = new Nest();
            nest.func_1 = test_1;
            
            var object:Object = new Object();
            object["test_1"] = test_1;
            
            var func:Function = new Function();
            func = test_1;
            
            var array:Array = [];
            array[0] = test_1;
            
            var list:Array = [0];
            
            var value:Number;
            
            
            
            ////////////////////////
            note.text += "------------------------------\n";
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_1();
            }
            note.end( ' msec: test_1();\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2();
            }
            note.end( ' msec: test_2();\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2(1);
            }
            note.end( ' msec: test_2(1);\n' );
            
            note.text += "------------------------------\n";
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_1.apply();
            }
            note.end( ' msec: test_1.apply(); \n' );
            
            
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2.apply();
            }
            note.end( ' msec: test_2.apply();\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_1.call();
            }
            note.end( ' msec: test_1.call(); \n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2.call();
            }
            note.end( ' msec: test_2.call();\n' );
            
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_1.call( NaN );
            }
            note.end( ' msec: test_1.call( NaN );\n' );
            
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2.call( NaN, 0 );
            }
            note.end( ' msec: test_2.call( NaN, 0 );\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_1.apply( NaN, [] );
            }
            note.end( ' msec: test_1.apply( NaN, [] );\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2.apply( NaN, [1] );
            }
            note.end( ' msec: test_2.apply( NaN, [1] );\n' );
            
            
            /////////////
            note.text += "------------------------------\n";
            
            var func_1:Function = test_1;
            var func_2:Function = test_2;
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                func_1();
            }
            note.end( ' msec: func_1();\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                func_1.call();
            }
            note.end( ' msec: func_1.call();\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                func_1.apply();
            }
            note.end( ' msec: func_1.apply();\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                func_2();
            }
            note.end( ' msec: func_2();\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                func_2.call( NaN, 1 );
            }
            note.end( ' msec: func_2.call( NaN, 1 );\n' );
            
            var argmentsArray:Array = [1];
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                func_2.apply( NaN, argmentsArray );
            }
            note.end( ' msec: func_2.apply( NaN, argmentsArray );\n' );
            
            
            note.text += "------------------------------\n";
            
           
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                value = 0;
            }
            note.end( ' msec: value = 0;\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                value = 10 * 3;
            }
            note.end( ' msec: value = 10 * 3;\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                value = 10 / 3;
            }
            note.end( ' msec: value = 10 / 3;\n' );
            
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                value = 10 % 3;
            }
            note.end( ' msec: value = 10 % 3;\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_2(list[0]);
            }
            note.end( ' msec: test_2(list[0]);\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                nest.test_1();
            }
            note.end( ' msec: test.test_1();\n' );
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                nest.func_1();
            }
            note.end( ' msec: test.func_1();\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                array[0]();
            }
            note.end( ' msec: array[0]();\n' );
            
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                object["test_1"]();
            }
            note.end( ' msec: object["test_1"](); \n' );
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_3( list );
            }
            note.end( ' msec: test_3( list );\n' );
            
            
            
            var testObject:Object = {x:0,title:"title",sprite:new Sprite()}
            //testObject.x = 0;
            
            note.start( "" );
            for( i=0;i<iMax;++i )
            {
                test_4( testObject );
            }
            note.end( ' msec: test_4( testObject );\n' );
            
            
            
            
        }

    }








    class Nest
    {
        public function Nest(){}
        public function test_1():void{}
        public var  func_1:Function;
    }














    /////////////////////////////////////////////////////////////////////
    import flash.display.Sprite;
    import flash.display.DisplayObjectContainer;
    import flash.events.Event;
    import flash.text.TextFormat;
    import flash.text.TextField;
    import flash.utils.getTimer;
    /**
    表示だけなら1行で書ける形の着想元
    ! MinimalComps の com.bit101.components.Component 
    https://github.com/minimalcomps/minimalcomps/blob/master/src/com/bit101/components/Component.as
    */
    class LLLNote extends Sprite
    {
        public function LLLNote( _root:DisplayObjectContainer=null, text:String=" ", x:Number=0,y:Number=0,widthX:Number=200,widthY:Number=100 )
        {
            textFormat = new TextFormat()
            textFormat.leftMargin = 0
            textFormat.rightMargin = 0
            textFormat.align = "left"
            textFormat.font = "_等幅"     //EXAMPLE  "_等幅"  "_ゴシック"  "_明朝 "  "_typewriter"  "_serif"  "_sans"
            textFormat.size = 10
            
            textField = new TextField()
            textField.background = false;
            textField.backgroundColor = 0xFFFFFF;
            textField.border = false;
            textField.borderColor = 0x000000;
            textField.textColor = 0x000000;
            textField.autoSize = "left";
            textField.selectable = false;
            textField.text = text;
            textField.defaultTextFormat = textFormat;
            
            this.text = text;
            this.x = x;
            this.y = y;
            //textField.width = widthX;
            //textField.height = widthY;
            
            addChild( textField );
            _root.addChild( this );
            
            addEventListener( Event.ENTER_FRAME, _onEnterFrame );
            
        }
        private function _onEnterFrame( event:Event ):void { textField.text = text; }
        
        public function start( startText:String = "" ):void{
            startTime = getTimer();
            text = text + startText;
        }
        public function end( endText:String = "" ):void{
            endTime = getTimer();
            elapsedTime = endTime - startTime;
            text = text + elapsedTime.toString() + endText;
        }
        
        public var elapsedTime:Number = 0;
        public var endText:String = "";
        public var endTime:Number = 0;
        public var startText:String = "";
        public var startTime:Number = 0;
        
        
        public var text:String
        public var textFormat:TextFormat
        public var textField:TextField
    }