BitmapDataの塗り

by shohei909 forked from BitmapDataの複製 (diff: 89)
普通の結果。
比べるものを間違えてる気がする。
♥0 | Line 167 | Modified 2010-11-03 09:21:34 | 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/53kO
 */

// forked from zahir's BitmapDataの複製
// forked from zahir's 速度比較用テンプレ
package {
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.display.BitmapData;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.utils.getTimer;
    
    public class FlashTest extends Sprite {
        private const LOOP:int = 30;
        private var t:TextField;
        public function FlashTest() {
            addChild( ( t= new TextField() ) ).width = t.height = 465;
            addEventListener( MouseEvent.CLICK, onClick );
            ex();
        }
        
        private function onClick( e:MouseEvent ):void
        {
            ex();
        }

        private function ex():void
        {
            var i:int = 0;                                      
            var time1:int;
            var time2:int;
            var recode:Array = [];
            var count:int = 0;
            t.text = "各 " + LOOP + " 回ループ\n\n";
            
            // ここから処理
            var w:int = 500;
            var h:int = 500;
            var bd:BitmapData = new BitmapData(w,h, false, 0xFFFFFF);
            var black:BitmapData = new BitmapData(w,h, false, 0x000000);
            var blackVec:BitmapData = new BitmapData(w,h, false, 0x000000);
            var white:BitmapData = new BitmapData(w,h, false, 0xFFFFFF);
            var rect:Rectangle = bd.rect;
            var p:Point = new Point();
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                bd.draw(black);
                bd.draw(white);
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "draw", time2, "" );
            
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                bd.merge(black,rect,p,255,255,255,255);
                bd.merge(white,rect,p,255,255,255,255);
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "merge", time2, "" );
            
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                bd.copyPixels(black,rect,p);
                bd.copyPixels(white,rect,p);
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "copyPixels", time2, "" );
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                bd.fillRect(rect,0x000000);
                bd.fillRect(rect,0xFFFFFF);
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "fillRect", time2, "" );
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd = black.clone();
                bd = white.clone();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "clone", time2, "" );
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                for( var x:uint = 0; x < w; x++ ){
                    for( var y:uint = 0; y < h; y++ ){
                        bd.setPixel(x,y, 0x000000);
                    }
                }
                for( x = 0; x < w; x++ ){
                    for( y = 0; y < h; y++ ){
                        bd.setPixel(x,y, 0xFFFFFF);
                    }
                }
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "setPixel", time2, "" );
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                var vec:Vector.<uint> = bd.getVector(rect);
                var l:uint = vec.length
                for( x = 0; x < l; x++ ){
                    vec[x] = 0x000000;
                }
                bd.setVector( rect, vec );
                vec = bd.getVector(rect);
                for( x = 0; x < l; x++ ){
                    vec[x] = 0xFFFFFF;
                }
                bd.setVector( rect, vec );
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "setVector", time2, "" );
            
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                bd.lock();
                bd = new BitmapData( w, h, false, 0x000000 );
                bd = new BitmapData( w, h, false, 0xFFFFFF );
                bd.unlock();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "new", time2, "" );
            
            
            bd.dispose();
            
            
            // ここまで処理
            
            /* templete
            time1 = getTimer();
            for( i = 0; i<LOOP; i++ )
            {
                
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "", time2, "" );
            */
            
            if( recode.length )
            {
                $( recode );
            }
            
            t.appendText("clickで再計算");
        }
        private function $( recode:Array ):void
        {
            var str:String = "";
            recode.sortOn( "time", Array.NUMERIC );
            var max:int = (recode[ recode.length - 1 ] as TimeData).time;
            for( var i:int = 0, len:int = recode.length; i<len; i++)
            {
                var data:TimeData = recode[i] as TimeData;
                var n:int = max / data.time * 100;
                str += data.title + " :: " + data.time + " ms\n\t\t最低速に比べて約 " + n + " %高速 \n";
                if(data.ms == "") str += "\n";
                else str += "\t\t" + data.ms  + "\n\n";
            }
            t.appendText( str );
        }
    }
}
import flash.geom.Matrix3D;
class TimeData
{
    public var title:String;
    public var time:int;
    public var ms:String; // なんか、おまけ要素
    public function TimeData( title:String = "", time:int = 0 , ms:String = "")
    {
        this.title = title;
        this.time = time;
        this.ms = ms;
    }
}