I have Grid which will render a calendar, and I'm provided with an ArrayList<CalendarEventEntity>
which contains events. Those events have to be highlighted in the grid.
As I have to fill the grid by my self I have something like this:
for( loop through the days of the month ){
Calendar eventDate = event.getDate();
// look for the events in the calendar that matchs this day
for(CalendarEventEntity event : events) {
// if there are events in this specific day
if( eventDate.get(Calendar.YEAR) == calendarMonth.get(Calendar.YEAR) &&
eventDate.get(Calendar.MONTH) == calendarMonth.get(Calendar.MONTH) &&
eventDate.get(Calendar.DAY_OF_MONTH) == dayIndex ) {
// highlight it!!!
}
}
}
This works fine, but it's too slow. So I want to speed it up! I added this before the inner for
:
// ignore dates which does not make part of this month or year
if( eventDate.get(Calendar.YEAR) < calendarMonth.get(Calendar.YEAR) ||
eventDate.get(Calendar.MONTH) < calendarMonth.get(Calendar.MONTH) ||
eventDate.get(Calendar.DAY_OF_MONTH) != DateIdx ) {
continue;
}
// stop when processing dates which are higher than this month or year
if( eventDate.get(Calendar.YEAR) > calendarMonth.get(Calendar.YEAR) ||
eventDate.get(Calendar.MONTH) > calendarMonth.get(Calendar.MONTH)
|| eventDate.get(Calendar.DAY_OF_MONTH) != DateIdx ) {
break;
}
and that made if faster, but it's still too slow. How can I improve this algorithm?