14 Replies Latest reply on Oct 30, 2007 7:24 AM by kglad

    Speed Tests Using Image Bytearrays

      This following information is very interesting and not at all what I would have expected.
      The following tests were conducted to discover the fastest ways to load in a 1600x1600 px image from the library and alter each pixel color one at a time.

      I have times each attempt 10 times and given an average below.
      The head code is used to set up the test.

      //HEAD CODE
      var imgWidth:int = 1600;
      var imgHeight:int = 1600;
      var libraryImage:BitmapData = new libImg(imgWidth,imgHeight);
      var rect:Rectangle = new Rectangle(0, 0, imgWidth,imgHeight);
      var libImgByteArray:ByteArray = libraryImage.getPixels(rect)
      var pixAlpha:int,pixRed:int,pixGreen:int,pixBlue:int;

      //BODY CODE - TEST 1
      pixAlpha = libImgByteArray.readByte();
      pixRed = libImgByteArray.readByte();
      pixGreen = libImgByteArray.readByte();
      pixBlue = libImgByteArray.readByte();
      //Timed results in milliseconds
      TOTAL TIME: 12092ms Average:1209.2ms for 10 passes.

      //BODY CODE - TEST 2
      var pos:int=0;
      var len:int=libImgByteArray.length;
      pixAlpha = libImgByteArray[pos];
      pixRed = libImgByteArray[pos+1];
      pixGreen = libImgByteArray[pos+2];
      pixBlue = libImgByteArray[pos+3];
      //Timed results in milliseconds
      TOTAL TIME: 3828ms Average:382.8ms for 10 passes.

      Now forgive me if I am wrong. But shouldn't the first example be faster? or at least just as fast? I would have thought so. Maybe I am missing something here. But most professionally written code I have seen will loop through a byteArray using the .position, .bytesAvailable and .readByte .readInt methods.

      How can it be that using readByte is so much slower than accessing the array by direct index. What is the point of even using readByte at all if it is 4 times slower?

      Is there a faster way I have not yet found. It seems strange to me that the inbuilt flash functionality such as floodFill and threshold would be required to test evey pixel.. yet those functions can do it in a very small fraction of the time it takes me to simply loop through and read each pixel colour. Is there a way I make this above code faster?