Saturday, March 9, 2013

BitmapFactory.decodeResource performance

While checking the performance of my app I've noticed BitmapFactory.decodeResource is relatively slow. So I've checked the alernative, instead of loading my PNG files as resources, putting them in /assets/ folder and loading using BitmapFactory.decodeStream.

Here are the test functions:

void decodeResource() {
 Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tile, null);
void decodeStream() {
 InputStream ins = null;
 try {
  ins = getAssets().open("tile.png");

  Bitmap originalBitmap = BitmapFactory.decodeStream(ins);
 } catch (final IOException e) {
 } finally {
  if (ins != null)
   try {
   } catch (IOException e) { }

Running both functions 50 times to load a small PNG file (230*230) on Nexus Galaxy running Android 4.2.2:
  • decodeResource: 1793ms 
  • decodeStream: 188ms 
Conclusion: decodeStream is almost x10 faster than decodeResource.