androidjodatimeandroid-traceview

Why is joda time get difference between hours taking so much of memory


I have the following function:

public static int getDiffHour(Date first) {
        int hoursBetween = Hours.hoursBetween(new LocalDate(first), new LocalDate()).getHours();
        return hoursBetween;
    }

I am trying to get difference in hours between two dates.

Here is my Traceview:

enter image description here

The function is taking crazy amounts of time and resource to execute - what do I miss here. My service is getting killed on 5.0.1 due to this enormous CPU time taken by this function.

Edit:

Digging down deeper, I find that 99% of the CPU time is taken by LocalDate init. Is there a way I can avoid that and supply a date precalculated?

Edit 2: After implementing Joda 2.6:

enter image description here


Solution

  • This issue might have been solved with the newest Joda-Time-release v2.6 because the class initialization of class DateTimeZone (where you observe huge amounts of time spent) no longer calls the method setProvider0(), see also this commit.