Wednesday, September 22, 2010

PAW Performance

To test the performance of PAW on Android I created a small web app for PAW Runtime which basically takes a small picture (80x120px) and transforms it into a grayscale and sepia image.

Web App


The transformation was done with native code (compiled into the PAW runtime) and also with pure BeanShell code to get a feel how BeanShell performs.
Main reasons for doing the test were the reports about bad BeanShell performance.

The algorithms used for BeanShell and native code  were identical.
Here is the BeabShell code:

import android.graphics.*;
import android.graphics.Bitmap.CompressFormat;

file = parameters.get("file");
filter = parameters.get("filter");
sepia = (filter != null && filter.equals("sepia")) ? true : false;

depth = 20;

if(file != null) {
 bitmap = BitmapFactory.decodeFile(file);

 if(bitmap != null) {
  bitmap2 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  w = bitmap.getWidth();
  h = bitmap.getHeight();
  for(x=0; x < w; x++) {
   for(y=0; y < h; y++) {
    c = bitmap.getPixel(x, y);

    r = Color.red(c);
    g = Color.green(c);
    b = Color.blue(c);
     
    gry = (r + g + b) / 3;
    r = g = b = gry;
    
    if(sepia) {
     r = r + (depth * 2);
     g = g + depth;
    }

    if(r > 255) {
      r = 255;
    }
    if(g > 255) {
      g = 255;
    }

    bitmap2.setPixel(x, y, Color.rgb(r, g, b));
   }
  }

  bos = new ByteArrayOutputStream();
  bitmap2.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
  byte[] bitmapdata = bos.toByteArray();

  request.sendResponse(bitmapdata, "image/png");
  request.out.flush();
  request.out.close();
 }
}


Here is the result after running the test:

Result


Actually BeanShell is much slower than I expected. But if you take into consideration what is happening behind the scenes it's not as surprising as it seems. There is a log of Java reflection, parsing, instantiations etc. going on.
Another result was, that the PAW server itself is performing nicely, which is a good sign.
So from a developer's point of view BeanShell code should be reduced to a minimum and native Android API should be used as much as possible.

What it also means is that, because the PAW Web App uses much BeanShell code, there is room for improvement...

To test it for yourself, download the latest PAW Runtime version and unpack the Image Manipulation ZIP file to your /sdcard/paw-runtime directory.

After that the following menu entry should be available in your PAW Runtime menu:

Menu Entry

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.