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

get/setPixel vs get/setVector(手抜き)

Get Adobe Flash player
by uwi 03 Jul 2009
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kjh1
 */

package {

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

public class Main extends Sprite
{
    static private const N:uint = 100;
    static private const M:uint = 1000;
    
    private function _init():void
    {
        _debug(
            "各テスト " + N + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
            "(誤差は多少生じます)\n"
        );
        
        // W*Hが小さいとsetVector(ずる)が有利げ
        var W : uint = 100;
        var H : uint = 100;
        
        _measure("getPixel/setPixel", function ():void
        {
            var bmd : BitmapData = new BitmapData(W, H, false);
            for (var i:uint = 0; i < N; i++) {
                bmd.lock();
                var x : int, y : int;
                for(var j : uint = 0;j < M;j++){
                    x = Math.random() * W;
                    y = Math.random() * H;
                    bmd.setPixel(x, y, bmd.getPixel(x, y) + 1);
                }
                bmd.unlock();
            }
            bmd.dispose();
        }); 
        
        _measure("getVector/setVector", function ():void
        {
            var bmd : BitmapData = new BitmapData(W, H, false);
            for (var i:uint = 0; i < N; i++) {
                bmd.lock();
                var vec : Vector.<uint> = bmd.getVector(bmd.rect);
                var x : int, y : int;
                for(var j : uint = 0;j < M;j++){
                    x = Math.random() * W;
                    y = Math.random() * H;
                    vec[x * H + y]++;
                }
                bmd.setVector(bmd.rect, vec);
                bmd.unlock();
            }
            bmd.dispose();
        });
        
        _measure("getVector/setVector(ずる)", function ():void
        {
            var bmd : BitmapData = new BitmapData(W, H, false);
            var S : uint = W * H;
            for (var i:uint = 0; i < N; i++) {
                bmd.lock();
                var vec : Vector.<uint> = bmd.getVector(bmd.rect);
                var x : int, y : int;
                for(var j : uint = 0;j < M;j++){
                    vec[int(Math.random() * S)]++;
                }
                bmd.setVector(bmd.rect, vec);
                bmd.unlock();
            }
            bmd.dispose();
        });
        
        _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();
        });
    }
}

}