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

BitmapDataをPixel単位で操作するベンチマーク

BitmapData#getVector() / setVector()
BitmapData#getPixels() / setPixels()
BitmapData#getPixel() / setPixel()
BitmapData#getPixel32() / setPixel32()

全部試してみた
・ByteArrayをreadByte()で走査するのはあまりに遅かったので省略
Get Adobe Flash player
by kaikoga 22 Aug 2011
/**
 * Copyright kaikoga ( http://wonderfl.net/user/kaikoga )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mJjaN
 */

// forked from kaikoga's forked from: copyPixels, getVector, getPixels ベンチマーク
// forked from yukoba's copyPixels, getVector, getPixels ベンチマーク
package {
    import flash.filters.ColorMatrixFilter;
    import flash.display.*;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.text.*;
    import flash.utils.*;
    
    [SWF(frameRate=60)]
    public class FlashTest extends Sprite {
        private var tf:TextField = new TextField();
        
        public function FlashTest() {
            addChild(tf);
            tf.autoSize = "left";
            var iterations:int = 2;
            setTimeout(function():void {
                getSetVectorTest(iterations);
                getSetPixelsTest(iterations);
                getSetPixelTest(iterations);
                getSetPixel32Test(iterations);
                getAltSetVectorTest(iterations);
                getAltSetPixelsTest1(iterations);
                getAltSetPixelsTest2(iterations);
                getAltSetPixelTest(iterations);
                getAltSetPixel32Test(iterations);
            }, 300);
        }
        
        private function getSetVectorTest(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                var vec:Vector.<uint> = bd1.getVector(bd1.rect);
                bd1.setVector(bd1.rect, vec);
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getSetVector\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getSetPixelsTest(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                var bytes:ByteArray = bd1.getPixels(bd1.rect);
                bytes.position = 0;
                bd1.setPixels(bd1.rect, bytes);
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getSetPixels\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getSetPixelTest(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                for (var y:int = 0; y < 600; y++) {
                    for (var x:int = 0; x < 800; x++) {
                        var pixel:uint = bd1.getPixel(x, y);
                        bd1.setPixel(x, y, pixel);
                    }
                }
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getSetPixel\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getSetPixel32Test(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                for (var y:int = 0; y < 600; y++) {
                    for (var x:int = 0; x < 800; x++) {
                        var pixel:uint = bd1.getPixel32(x, y);
                        bd1.setPixel32(x, y, pixel);
                    }
                }
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getSetPixel32\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }
        
        private function getAltSetVectorTest(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                var vec:Vector.<uint> = bd1.getVector(bd1.rect);
                var jc:int = vec.length;
                for (var j:int = 0; j < jc; j++) {
                    var pixel:uint = vec[j];
                    var a:int = (pixel >> 24) & 0xff;
                    var r:int = (pixel >> 16) & 0xff;
                    var g:int = (pixel >> 8) & 0xff;
                    var b:int = pixel & 0xff;
                    r = (r + 1) & 0xff;
                    g = (g + 1) & 0xff;
                    b = (b + 1) & 0xff;
                    pixel = (a << 24) | (r << 16) | (g << 8) | b;
                    vec[j] = pixel;
                }
                bd1.setVector(bd1.rect, vec);
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getAltSetVector\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getAltSetPixelsTest1(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                var bytes:ByteArray = bd1.getPixels(bd1.rect);
                var jc:int = bytes.length;
                for (var j:int = 0; j < jc;) {
                    var p:int = j;
                    var a:int = bytes[p++];
                    var r:int = bytes[p++];
                    var g:int = bytes[p++];
                    var b:int = bytes[p++];
                    r = (r + 1) & 0xff;
                    g = (g + 1) & 0xff;
                    b = (b + 1) & 0xff;
                    bytes[j++] = a;
                    bytes[j++] = r;
                    bytes[j++] = g;
                    bytes[j++] = b;
                }
                bytes.position = 0;
                bd1.setPixels(bd1.rect, bytes);
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getAltSetPixelsByArrayAccess\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getAltSetPixelsTest2(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                var bytes:ByteArray = bd1.getPixels(bd1.rect);
                bytes.position = 0;
                var jc:int = bytes.length;
                for (var j:int = 0; j < jc; j += 4) {
                    var pixel:uint = bytes.readUnsignedInt();
                    var a:int = (pixel >> 24) & 0xff;
                    var r:int = (pixel >> 16) & 0xff;
                    var g:int = (pixel >> 8) & 0xff;
                    var b:int = pixel & 0xff;
                    r = (r + 1) & 0xff;
                    g = (g + 1) & 0xff;
                    b = (b + 1) & 0xff;
                    pixel = (a << 24) | (r << 16) | (g << 8) | b;
                    bytes.position = j;
                    bytes.writeUnsignedInt(pixel);
                }
                bytes.position = 0;
                bd1.setPixels(bd1.rect, bytes);
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getAltSetPixelsByReadUnsignedUint\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getAltSetPixelTest(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                for (var y:int = 0; y < 600; y++) {
                    for (var x:int = 0; x < 800; x++) {
                        var pixel:uint = bd1.getPixel(x, y);
                        var a:int = (pixel >> 24) & 0xff;
                        var r:int = (pixel >> 16) & 0xff;
                        var g:int = (pixel >> 8) & 0xff;
                        var b:int = pixel & 0xff;
                        r = (r + 1) & 0xff;
                        g = (g + 1) & 0xff;
                        b = (b + 1) & 0xff;
                        pixel = (a << 24) | (r << 16) | (g << 8) | b;
                        bd1.setPixel(x, y, pixel);
                    }
                }
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getAltSetPixel\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }

        private function getAltSetPixel32Test(iterations:int):void {
            var bd1:BitmapData = new BitmapData(800, 600, true, 0x80FF0000);
            var start:int = getTimer();
      
            for (var i:int = 0; i < iterations; i++) {
                for (var y:int = 0; y < 600; y++) {
                    for (var x:int = 0; x < 800; x++) {
                        var pixel:uint = bd1.getPixel32(x, y);
                        var a:int = (pixel >> 24) & 0xff;
                        var r:int = (pixel >> 16) & 0xff;
                        var g:int = (pixel >> 8) & 0xff;
                        var b:int = pixel & 0xff;
                        r = (r + 1) & 0xff;
                        g = (g + 1) & 0xff;
                        b = (b + 1) & 0xff;
                        pixel = (a << 24) | (r << 16) | (g << 8) | b;
                        bd1.setPixel32(x, y, pixel);
                    }
                }
            }
            
            var time:int = getTimer() - start;
            tf.appendText("getAltSetPixel32\ntime = " + time / iterations + "ms\n\n");
            
            bd1.dispose();
        }
    }
}