Arrayの比較

by shohei909 forked from 速度測定テンプレート2 (diff: 41)
♥0 | Line 132 | Modified 2011-08-30 20:53:53 | MIT License
play

ActionScript3 source code

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

// forked from zahir's 速度測定テンプレート2
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 = 1e4;
        
        private function exec(first:Boolean = false):void
        {
            var i:int = 0;
            var j:int = 0;
            var time1:int;
            var time2:int;
            var count:int = 0;
            var c:int;
            //Arrayのリスト
            //2つのArrayが長さ100の配列を10こ持っていて同じ番号の数字は一致する
            var a:Array = [ [],[] ]
            for( i = 0; i < 10; i++ ){
                a[0][i] = []; a[1][i] = []; 
                for( j = 0; j < 100; j++ ){
                    a[0][i][j] = a[1][i][j] = ( Math.random() * 3 ) >> 0;
                }
                a[0][i].sort( Array.NUMERIC );
                a[1][i].sort( Array.NUMERIC );
            }

            
            
            if(first) recodes = [];
            
            // ここから処理
            time1 = getTimer();
            c = 0;
            for(i=0;i<LOOP;i++){
                c += ( a[0][(Math.random()*10) >> 0].toString() == a[1][(Math.random()*10) >> 0].toString())
            }
            time2 = getTimer() - time1;
            if(first) recodes[count++] = new RecodeData( "toString", time2, "" );
            else (recodes[count++] as RecodeData).time = time2;
            
            time1 = getTimer();
            c = 0;
            lp: for(i=0;i<LOOP;i++){
                var a0:Array = a[0][(Math.random()*10) >> 0];
                var a1:Array = a[1][(Math.random()*10) >> 0];
                var l:int, f:Boolean = false;
                if( (l = a0.length) == a1.length ){
                    for( j = 0; j < l; j++ ){
                        if( a0[j] != a1[j] ){ f = true; break; }
                    }
                }
                c += !f;
            }
            time2 = getTimer() - time1;
            if(first) recodes[count++] = new RecodeData( "", time2, "" );
            else (recodes[count++] as RecodeData).time = time2;
            
            // ここまで処理
            
            /* TEMPLETE
            
            time1 = getTimer();
            for(i=0;i<LOOP;i++)
            {
                
            }
            time2 = getTimer() - time1;
            if(first) recodes[count++] = new RecodeData( "", 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++;
    }
}