I am using traceview and DDMS to analyse my operation. The following is my traceview as displayed in DDMS:
According to the above, the LoaderClass.months()
function takes about 51% of the CPU time and 798.80 System time. I have reproduced this function below:
public int months(String date) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
try {
d1 = f.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Date d2 = new Date();
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
int diff = 0;
if (c2.after(c1)) {
while (c2.after(c1)) {
c1.add(Calendar.MONTH, 1);
if (c2.after(c1)) {
diff++;
}
}
} else if (c2.before(c1)) {
while (c2.before(c1)) {
c1.add(Calendar.MONTH, -1);
if (c1.before(c2)) {
diff--;
}
}
}
return diff;
}
Below is a more detailed view of the method as on DDMS: