Showing posts with label Performance. Show all posts
Showing posts with label Performance. Show all posts

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);
 originalBitmap.recycle();
}
void decodeStream() {
 InputStream ins = null;
 try {
  ins = getAssets().open("tile.png");

  Bitmap originalBitmap = BitmapFactory.decodeStream(ins);
  originalBitmap.recycle();
 } catch (final IOException e) {
  e.printStackTrace();
 } finally {
  if (ins != null)
   try {
    ins.close();
   } 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.

Monday, May 28, 2007

Fast DataTable loading from IDataReader

I was using the Load method of DataTable to load data from IDataReader the other day, and while profiling the application I found out that this is my program bottleneck.

I've just wrote a quick & dirty function which does the same task much faster:

private static DataTable ReaderToDataTable(IDataReader reader)
{
int fieldCount = reader.FieldCount;
DataTable result = new DataTable("Query Result");
for (int i = 0; i < reader.FieldCount; i++)
{
DataColumn column = result.Columns.Add(reader.GetName(i));
column.DataType = reader.GetFieldType(i);
}

object[] row = new object[fieldCount];
while (reader.Read())
{
reader.GetValues(row);
result.Rows.Add(row);
}

return result;
}