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: [最適化 Tips] Number, int, uint の計算による処理速度の違い

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

// forked from tail_y's forked from: [最適化 Tips] Number, int, uint の計算による処理速度の違い
// forked from muta244's [最適化 Tips] Number, int, uint の計算による処理速度の違い
package {

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

public class Main extends Sprite
{
    static private const _NUM_TIMES:int = 5000000;
    
    private function _init():void
    {
        _debug(
            "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
            "(誤差は多少生じます)\n"
        );
        
        
        _measure("ループのみ", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                
            }
        });
        
        _measure("Number の演算(1回)", function ():void
        {
            var n1:Number = 0x5555;
            var o1:Number;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o1 = n1 + n1;
            }
        });
        _measure("Number の演算(2回)", function ():void
        {
            var n1:Number = 0x5555;
            var o1:Number;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o1 = n1 + n1 - n1;
            }
        });
        _measure("Number の演算(1回+1回)", function ():void
        {
            var n1:Number = 0x5555;
            var o1:Number;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o1 = n1 + n1;
                o1 -= n1;
            }
        });
        
        _measure("int の演算(1回)", function ():void
        {
            var n2:int = 0x5555;
            var o2:int;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o2 = n2 + n2;
            }
        });
        _measure("int の演算(2回)", function ():void
        {
            var n2:int = 0x5555;
            var o2:int;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o2 = n2 + n2 - n2;
            }
        });
        _measure("int の演算(1回+1回)", function ():void
        {
            var n2:int = 0x5555;
            var o2:int;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o2 = n2 + n2;
                o2 = o2 - n2;
            }
        });
        
        _measure("uint の演算(1回)", function ():void
        {
            var n3:uint = 0xaaaa;
            var o3:uint;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o3 = n3 + n3;
            }
        });
        _measure("uint の演算(2回)", function ():void
        {
            var n3:uint = 0xaaaa;
            var o3:uint;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o3 = n3 + n3 - n3;
            }
        });
        _measure("uint の演算(1回+1回)", function ():void
        {
            var n3:uint = 0xaaaa;
            var o3:uint;
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                o3 = n3 + n3;
                o3 -= n3;
            }
        });
        
        _debug("\n以下大きい数値の型についての調査");
        _debug("getQualifiedClassName(1 + 1) = " + getQualifiedClassName(1 + 1));
        var v1:* = 0xfffffff;
        var v2:* = 0x10000000;
        var v3:* = 0xfffffff;
        _debug("getQualifiedClassName(0xfffffff) = " + getQualifiedClassName(0xfffffff));
        _debug("getQualifiedClassName(0x10000000) = " + getQualifiedClassName(0x10000000));
        _debug("getQualifiedClassName(v1) = " + getQualifiedClassName(v1));
        _debug("getQualifiedClassName(v2) = " + getQualifiedClassName(v2));
        _debug("getQualifiedClassName(v1 + 1) = " + getQualifiedClassName(v1 + 1));
        var i1:int = 0xfffffff;
        _debug("getQualifiedClassName(i1) = " + getQualifiedClassName(i1));
        _debug("getQualifiedClassName(i1 + 1) = " + getQualifiedClassName(i1 + 1));
        _debug("\n結果については言及しませんので, 各自ご判断ください.");
    }
    
    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 - 60;
        _field.x = 20;
        _field.y = 60;
        _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 = 20;
        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();
        });
    }
}

}