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

concat() vs push.apply()

配列の連結の速度比較をやっています。

concat()が遅いので、ちょっとだけ固まります。
配列が長くなるとconcatはどんどん遅くなるみたいです。
Get Adobe Flash player
by shohei909 08 Jun 2011

    Talk

    focus at 04 Dec 2010 21:39
    a = b.concat() should be faster than for(i=0;i<LOOP;i++) { a=a.concat(b); } ;)
    focus at 04 Dec 2010 21:44
    And, yes, it will be even faster than for(i=0;i<LOOP;i++) { a.push.apply(null,b); }

    Tags

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

// forked from shohei909's concat() vs push.apply()
// forked from zahir's 速度測定テンプレート2

// 配列の連結の速度比較をやっています。
// 
// concat()が遅いので、ちょっとだけ固まります。
// 配列が長くなるとconcatはどんどん遅くなるみたいです。
package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.utils.getTimer;

    [SWF(width="465",height="465")]
    public class Instrumentation extends Sprite
    {
        private var event:Event;
        private var runsLen:int = 10;
        private var runCount:int = 0;
        private var t:TextField;
        private var recodes:Array;
        
        private const LOOP:int = 3000;
        
        private function exec(first:Boolean = false):void
        {
            var i:int = 0;
            var time1:Number;
            var time2:Number;
            var count:int = 0;
            if(first) recodes = [];
            
            // ここから処理
            var a:Array = []
            var b:Array = [0,1,2,3,4,5,6,7,8,9];
            
            
            
            time1 = getTimer();
            for(i=0;i<LOOP;i++)
            {
                a.push.apply(null,b);
            }
            time2 = getTimer() - time1;
            if(first) recodes[count++] = new RecodeData( "push.apply", time2, "" );
            else (recodes[count++] as RecodeData).time = time2;
            
            a = [];
            
            time1 = getTimer();
            for(i=0;i<LOOP;i++)
            {
                a=b.concat(a);
            }
            time2 = getTimer() - time1;
            if(first) recodes[count++] = new RecodeData( "short.concat(long)", time2, "" );
            else (recodes[count++] as RecodeData).time = time2;
            
            a = []; 
             time1 = getTimer();
            for(i=0;i<LOOP;i++)
            {
                a=a.concat(b);
            }
            time2 = getTimer() - time1;
            if(first) recodes[count++] = new RecodeData( "long.concat(short)", time2, "" );
            else (recodes[count++] as RecodeData).time = time2;
             
            
            runCount++;
            var _loop:String = LOOP.toString().replace( /([0-9]+?)(?=(?:[0-9]{3})+$)/g , "$1," );
            t.text = "各 " + _loop + " 回ループ を " + runCount  + " 回実行しました。\n\n";
            
            if( recodes.length ) listUp();
            
            t.appendText("clickで再計算");
            
            if(event){
                runsLen--;
                trace(runsLen);
                if(runsLen <= 0) return;
                dispatchEvent( event );
            }
        }
        
        public function Instrumentation()
        {
            addChild( (t = new TextField() ) ).width = t.height = 465;
            
            addEventListener( MouseEvent.CLICK, onClick);
            
            /* ある程度回数を回したいとき用
            
            addEventListener("ex", onRun);
            event = new Event("ex");
            
            //*/
            
            exec(true);
        }
        private function onClick( e:MouseEvent ):void
        {
            exec();
        }
        
        private function onRun(e:Event):void
        {
            exec();
        }
        
        private function listUp():void
        {
            var str:String = "";
            var recode:Array = copy();
            recode.sortOn( "time", Array.NUMERIC );
            
            var max:int = (recode[ recode.length - 1 ] as RecodeData).time;
            for( var i:int = 0, len:int = recode.length; i<len; i++)
            {
                var data:RecodeData = recode[i] as RecodeData;
                var n:int = max / data.time * 100;
                str += data.title + " :: " + data.time + " ms\n\t\t最低速に比べて約 " + n + " %高速 \n";
                if(data.message == "") str += "\n";
                else str += "\t\t" + data.message  + "\n\n";
            }
            t.appendText( str );
        }
        
        private function copy():Array
        {
            var arr:Array = [];
            for(var i:int = 0, len:int = recodes.length; i<len; i++){
                arr[i] = recodes[i];
            }
            return arr;
        }
    }
}
class RecodeData
{
    private var count:int = 0;
    public var title:String = "";
    private var _time:uint;
    public var message:String = "";
    
    public function RecodeData( title:String, time:int, message:String = "" )
    {
        this.title = title;
        this.time = time;
        this.message = message;
    }
    
    public function get time():int
    {
        return _time / count;
    }
    
    public function set time( value:int ):void
    {
        _time += value;
        count++;
    }
}