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

flash on 2010-5-17

Get Adobe Flash player
by uwi 17 May 2010
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ia58
 */

package {

import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
import flash.system.*;

public class Main extends Sprite
{
    static private const N:uint = 300000;
    static private const M:uint = 30;
    
    private function _init():void
    {
        _debug(
            "各テスト " + N + " 回処理させた計算結果(アクセスは" + N + "*" + M + "回) [単位 : ミリ秒]"
        );
        
        _measure("アクセスループのみ", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            }
	        	}
        });
        
        testObject();
        _debug("--------------------");
        testClass();
        _debug("--------------------");
        testNumber();
        _debug("--------------------");
        testUint();
        _debug("--------------------");
        _debug("アクセスはそれほど変わらず、格納は中身が単純になるほどVector有利になる印象");
        _debug("ArrayとVectorの順番を入れ替えると結果が変わるのもある"); 
    }
    
    private function testObject() : void
    {
        _measure("ループのみ(Object)", function ():void
        {
            for (var i:uint = 0; i < N; i++) {
                {i:i};
            }
        });
        
        _measure("Array格納(Object)", function ():void
        {
        		var ar : Array = new Array(N);
            for (var i:uint = 0; i < N; i++) {
            		ar[i] = {i:i};
            }
        });
        
        _measure("Vector格納(Object)", function ():void
        {
        		var vec : Vector.<Object> = new Vector.<Object>(N);
            for (var i:uint = 0; i < N; i++) {
                vec[i] = {i:i};
            }
        });
        
        var i : uint;
    		var gvec : Vector.<Object> = new Vector.<Object>(N);
        for (i = 0; i < N; i++) {
            gvec[i] = {i:i};
        }
            
    		var gar : Array = new Array(N);
    		for (i = 0; i < N; i++) {
            gar[i] = {i:i};
        }
        
        _measure("Arrayアクセス(Object)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : Object = gar[i];
	            }
	        	}
        });
        
        _measure("Vectorアクセス(Object)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : Object = gvec[i];
	            }
	        	}
        });
    }
    
    private function testClass() : void
    {
        _measure("ループのみ(Class)", function ():void
        {
            for (var i:uint = 0; i < N; i++) {
                new A();
            }
        });
        
        _measure("Array格納(Class)", function ():void
        {
        		var ar : Array = new Array(N);
            for (var i:uint = 0; i < N; i++) {
            		ar[i] = new A();
            }
        });
        
        _measure("Vector格納(Class)", function ():void
        {
        		var vec : Vector.<A> = new Vector.<A>(N);
            for (var i:uint = 0; i < N; i++) {
                vec[i] = new A();
            }
        });
        
        var i : uint;
    		var gvec : Vector.<A> = new Vector.<A>(N);
        for (i = 0; i < N; i++) {
            gvec[i] = new A();
        }
            
    		var gar : Array = new Array(N);
    		for (i = 0; i < N; i++) {
            gar[i] = new A();
        }
            
        _measure("Arrayアクセス(Class)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : A = gar[i];
	            }
	        	}
        });
        
        _measure("Vectorアクセス(Class)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : A = gvec[i];
	            }
	        	}
        });
    }
    
    private function testNumber() : void
    {
            
        _measure("ループのみ(Number)", function ():void
        {
            for (var i:uint = 0; i < N; i++) {
                Math.sqrt(i);
            }
        });
        
        _measure("Array格納(Number)", function ():void
        {
        		var ar : Array = new Array(N);
            for (var i:uint = 0; i < N; i++) {
            		ar[i] = Math.sqrt(i);
            }
        });
        
        _measure("Vector格納(Number)", function ():void
        {
        		var vec : Vector.<Number> = new Vector.<Number>(N);
            for (var i:uint = 0; i < N; i++) {
                vec[i] = Math.sqrt(i);
            }
        });
        
        var i : uint;
    		var gvec : Vector.<Number> = new Vector.<Number>(N);
        for (i = 0; i < N; i++) {
            gvec[i] = Math.sqrt(i);
        }
        
    		var gar : Array = new Array(N);
    		for (i = 0; i < N; i++) {
            gar[i] = Math.sqrt(i);
        }
        
        _measure("Arrayアクセス(Number)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : Number = gar[i];
	            }
	        	}
        });
        
        _measure("Vectorアクセス(Number)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : Number = gvec[i];
	            }
	        	}
        });
    }
    
    
    private function testUint() : void
    {
        _measure("ループのみ(uint)", function ():void
        {
            for (var i:uint = 0; i < N; i++) {
            		i;
            }
        });
        
        _measure("Array格納(uint)", function ():void
        {
        		var ar : Array = new Array(N);
            for (var i:uint = 0; i < N; i++) {
            		ar[i] = i;
            }
        });
        
        _measure("Vector格納(uint)", function ():void
        {
        		var vec : Vector.<uint> = new Vector.<uint>(N);
            for (var i:uint = 0; i < N; i++) {
                vec[i] = i;
            }
        });
        
        var i : uint;
    		var gvec : Vector.<uint> = new Vector.<uint>(N);
        for (i = 0; i < N; i++) {
            gvec[i] = i;
        }
        
    		var gar : Array = new Array(N);
    		for (i = 0; i < N; i++) {
            gar[i] = i;
        }
            
        _measure("Arrayアクセス(uint)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : uint = gar[i];
	            }
	        	}
        });
        
        _measure("Vectorアクセス(uint)", function ():void
        {
        		for(var j : uint = 0;j < M;j++){
	            for (var i:uint = 0; i < N; i++) {
	            		var t : uint = gvec[i];
	            }
	        	}
        });
    }
    private var _field:TextField;
    private var _time:uint;
    
    public function Main():void
    {
        _setup();
        _init();
    }
    
    private function _measure(title:String, func:Function, ...params):void
    {
        _time = getTimer();
        func.apply(null, params);
        _time = getTimer() - _time;
        
        _debug("[ " + title + " ] --> " + _time + " ms");
    }
    
    private function _debug(log:String):void
    {
        _field.appendText(log + "\n");
    }
    
    private function _setup():void
    {
        _field = new TextField();
        _field.width = stage.stageWidth - 40;
        _field.height = stage.stageHeight - 40;
        _field.x = 20;
        _field.y = 40;
        _field.multiline = true;
        _field.wordWrap = true;
        
        var format:TextFormat = _field.defaultTextFormat;
        format.font = "_sans";
        _field.defaultTextFormat = format;
        
        addChild(_field);
        
        var button:Sprite = new Sprite();
        button.graphics.lineStyle(1, 0xBBBBBB);
        button.graphics.beginFill(0xEEEEEE);
        button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
        button.graphics.endFill();
        
        addChild(button);
        
        button.x = 20;
        button.y = 10;
        button.mouseChildren = false;
        button.buttonMode = true;
        
        var field:TextField = new TextField();
        field.width = 100;
        field.height = 20;
        field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
        
        button.addChild(field);
        
        button.addEventListener(MouseEvent.CLICK, function ():void
        {
            _field.text = "";
            _init();
        });
    }
}

}

class A
{
}