Lisa Anne
Lisa Anne

Reputation: 4595

Java/Android: I don't understand why this is slower than expected

I have this method:

@DebugLog
private synchronized int insertInOrderedFromBottom(ItemWithTime itemWithTime, ArrayList<ItemWithTime> array) {
    long start = System.currentTimeMillis();
    if (itemWithTime == null || itemWithTime.getDateTime() == null) return -1;
    if (array == null) return -1;
    int arraySize = array.size();
    for (int i = arraySize - 1; i >= 0; i--) {
        if (itemWithTime.getDateTime().isBefore(array.get(i).getDateTime())) {
            i++;
            array.add(i, itemWithTime);
            long end = System.currentTimeMillis();
            Log.d(TAG,"insertInOrderedFromBottom inside took "+(end-start));
            return i;
        }
    }
    array.add(itemWithTime);
    long end = System.currentTimeMillis();
    Log.d(TAG,"insertInOrderedFromBottom inside took "+(end-start));
    return array.size();
}

Now, you see the log statements I use to measure the time it takes this method to complete:

I call the method like this:

start = System.currentTimeMillis();
insertInOrderedFromBottom(datum, items);
end = System.currentTimeMillis();
Log.d(TAG, "insertInOrderedFromBottom invoication took " + (end - start) + " ms");

What is happening? What am I missing here?

EDIT

I have tried without synchronized and no difference.

Upvotes: 0

Views: 64

Answers (1)

user458577
user458577

Reputation:

The android log function is fairly slow. So if you have a log inside the inner loop that prints a time difference then that in itself will take a number of milliseconds. The last output timing thus reflects the time necessary to perform the Log.d in the inner loop

Upvotes: 3

Related Questions