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

Dictionary と Object の速度検証

キーがない場合のアクセス速度はかなり気分屋。
Get Adobe Flash player
by bkzen 07 Jan 2011
  • Related works: 1
  • Talk

    bkzen at 07 Jan 2011 10:37
    不思議なことがわかった。 5e5 回 実行 1回目、 Dict: 15921 [ms], Obj: 2063 [ms] 2回目、 Dict: 2119 [ms], Obj: 2126 [ms] Dictionary は 2回目がやたら早い・・・。 1回ずつ新しい Dictionary を作っているはずなのに・・・。
    bkzen at 07 Jan 2011 11:13
    clockmaker さんの助言で逆にしてみたら、挙動が逆になった。 http://wonderfl.net/c/3fLR

    Tags

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

package  
{
    import com.bit101.components.HBox;
    import com.bit101.components.InputText;
    import com.bit101.components.Label;
    import com.bit101.components.PushButton;
    import com.bit101.components.RadioButton;
    import com.bit101.components.Style;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.utils.Dictionary;
    import flash.utils.getTimer;
    /**
     * Dictionary や Object のアクセス速度の検証。
     * キーがない場合のアクセス速度はかなり気分屋。
     * @author jc at bk-zen
     */
    [SWF (backgroundColor = "0xFFFFFF", frameRate = "30", width = "465", height = "465")]
    public class Test3 extends Sprite 
    {
        private var dict:     Dictionary;
        private var obj:      Object;
        private var labelD:   InputText;
        private var labelO:   InputText;
        private var labelN:   InputText;
        private var radios:   HBox;
        private var keys:     HBox;
        private var n:        int;
        private var key:      String;
        private var startBtn: PushButton;
        
        public function Test3() 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e: Event = null): void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //
            Style.fontName = "Verdana";
            Style.embedFonts = false;
            Style.fontSize = 10;
            Style.BACKGROUND = 0xEEEEEE;
            
            var i: int, hbox: HBox;
            keys = new HBox(this, 100, 80);
            new RadioButton(keys, 0, 0, "キー無し", true,  onClickK).tag = 0;
            new RadioButton(keys, 0, 0, "キーあり", false, onClickK).tag = 1;
            for (i = 0; i < keys.numChildren; i ++) RadioButton(keys.getChildAt(i)).groupName = "key";
            
            radios = new HBox(this, 100, 100);
            new RadioButton(radios, 0, 0, "7e5", false, onClickN);
            new RadioButton(radios, 0, 0, "6e5", false, onClickN);
            new RadioButton(radios, 0, 0, "5e5", true,  onClickN);
            new RadioButton(radios, 0, 0, "4e5", false, onClickN);
            new RadioButton(radios, 0, 0, "3e5", false, onClickN);
            new RadioButton(radios, 0, 0, "2e5", false, onClickN);
            new RadioButton(radios, 0, 0, "1e5", false, onClickN);
            n = 500000, key = "moja";
            trace(n, int("5e5"));
            
            startBtn = new PushButton(this, 100, 120, "start", onClickStart);
            
            hbox = new HBox(this, 100, 160);
            new Label(hbox, 0, 0, "繰り返し回数\t : ");
            labelN = new InputText(hbox, 0, 0, "500000 回");
            labelN.enabled = false;
            
            hbox = new HBox(this, 100, 180);
            new Label(hbox, 0, 0, "Dictionary\t : ");
            labelD = new InputText(hbox, 0, 0, "****[ms]");
            
            hbox = new HBox(this, 100, 200);
            new Label(hbox, 0, 0, "Object\t\t : ");
            labelO = new InputText(hbox, 0, 0, "****[ms]");
            
        }
        
        private function onClickStart(e: Event):void 
        {
            startBtn.enabled = keys.enabled = radios.enabled = false;
            labelD.text = labelO.text = "wait";
            wait(checkInit);
        }
        
        private function onClickK(e: Event):void 
        {
            var r: RadioButton = e.target as RadioButton;
            key = r.tag ? "piyo" : "moja";
        }
        
        private function onClickN(e: Event):void 
        {
            var r: RadioButton = e.target as RadioButton;
            n = int(r.label);
            labelN.text = n + " 回";
        }
        
        private function checkInit(): void
        {
            dict = new Dictionary();
            obj  = { };
            var i: int, s: String;
            for (i = 0; i < n; i ++)
            {
                dict[s = "piyo" + i] = "moja";
                obj[ s             ] = "moja";
            }
            labelD.text = "check...";
            wait(checkDict);
        }
        
        private function checkDict():void 
        {
            var t: int, i: int, s: String;
            t = getTimer();
            for (i = 0; i < n; i ++) { s = dict[key + i]; }
            labelD.text = (getTimer() - t) + " [ms]";
            labelO.text = "check...";
            wait(checkObj);
        }
        
        private function checkObj():void 
        {
            var t: int, i: int, s: String;
            t = getTimer();
            for (i = 0; i < n; i ++) { s = obj[ key + i]; }
            labelO.text = (getTimer() - t) + " [ms]";
            wait(checkEnd);
        }
        
        private function checkEnd():void 
        {
            startBtn.enabled = keys.enabled = radios.enabled = true;
        }
        
        private function wait(closure: Function): void
        {
            callLater(function(): void
            {
                callLater(closure);
            });
        }
    }
}
//package com.bkzen.utils 
//{
    
    /**
     * あとで一回呼ぶ関数。
     * @author jc at bk-zen.com
     */
    //public function callLater(closure: Function): void
    function callLater(closure: Function): void
    {
        Ticker.add(closure);
    }
//}
import flash.display.Shape;
import flash.events.Event;
internal class Ticker
{
    internal static function add(closure: Function): void
    {
        if (handlers.indexOf(closure) < 0) handlers[cnt++] = closure;
    }
    
    private static var ticker: Shape;
    private static var cnt: int;
    private static var handlers: Array;
    {
        handlers = [];
        ticker = new Shape();
        ticker.addEventListener(Event.ENTER_FRAME, enter);
        //ticker.addEventListener(Event.EXIT_FRAME, enter);
    }
    private static function enter(e:Event):void 
    {
        if (cnt == 0) return;
        var arr: Array = handlers;
        handlers = [], cnt = 0;
        var n: int = arr.length;
        for (var i:int = 0; i < n; i++) 
        {
            arr[i]();
        }
    }
}