1

I've got a custom Image-cache which basically does the following:

I call it like this:

myImageLoader.imageForImageView(R.id.image_I_want_to_assign, myImageView);

With the method:

public void imageForImageView(int id, ImageView iv){
    Bitmap bitmap = cachedBitmaps.get(id);

    // Does this Bitmap already exist in the HashMap?
    if(bitmap != null)
        // If the given ImageView isn't null
        if(iv != null)
            // Then assign the Bitmap to the ImageView
            iv.setImageBitmap(bitmap);
    // If it doesn't exists yet:
    else{
        // Create it locally using the BitmapFactory
        bitmap = BitmapFactory.decodeResource(context.getResources(), id);
        // Does the Drawable with this given Resource-ID exist?
        // Yes:
        if(bitmap != null){
            // Put it in the HashMap
            cachedBitmaps.put(id, bitmap);
            // and assign it to the ImageView
            if(iv != null)
                iv.setImageBitmap(bitmap);

            L.Log(TAG, "New Image added to the list: " + context.getResources().getResourceEntryName(id), LogType.INFO);
        }
        // No:
        else
            // Use the default Resource-ID instead
            if(iv != null)
                iv.setImageBitmap(cachedBitmaps.get(DEFAULT_CHECKBOX));
    }
}

My question is whether I should just keep:

if(bitmap != null)
    if(iv != null)
        iv.setImageBitmap(bitmap);

Or check if this ImageView doesn't already have this Bitmap attached, and if not add it:

if(bitmap != null)
    if(iv != null && ((BitmapDrawable)iv.getDrawable()).getBitmap() != bitmap)
        iv.setImageBitmap(bitmap);

What is faster? Checking if the ImageView already had this Bitmap by using getDrawable().getBitmap including the BitmapDrawable cast, or just set it without checking even if it already had this same Bitmap as Image?


EDIT 1

If someone knows a way to check the time difference between the two options with some kind of UnitTest (10.000 times to prevent insecurities in the nano/microseconds) I would appreciate it. Or is there some kind of Debugger-style timer Eclipse-Plugin, so I can just set two timer-points (start and end) and it will give me a log of the time in between them?

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • So you don't know how to measure time between methods calls? – eleven Jul 23 '14 at 13:15
  • @Foxinsocks Nope, never used it. I guess I could start a timer after `if(bitmap != null)` and stop it again after `iv.setImageBitmap(bitmap)` both with and without the if, though I think that the mere nano/micro-seconds it will produce will vary too much. Is there a way to UnitTest this with for example calling it 10.000 times and then see the time-difference? How would you approach timing it? – Kevin Cruijssen Jul 23 '14 at 13:20
  • Yes, you can do it. But this is another question. So either create new question or edit this one. – eleven Jul 23 '14 at 13:24

0 Answers0