public class Main implements EntryPoint {
@Override
public void onModuleLoad() {
final int count = 100000;
final Date prev = new Date(0);
final Date next = new Date(0);
long optTime = 0;
long origTime = 0;
for (int i = 0; i<count;i++) {
next.setTime(prev.getTime()+1000*60*60*24);
final long startOrig = System.currentTimeMillis ();
final int origResult = CalendarUtil.getDaysBetween (prev, next);
origTime+= System.currentTimeMillis ()-startOrig;
final long startOpt = System.currentTimeMillis ();
final int optResult = getDaysBetween (prev, next);
optTime+= System.currentTimeMillis ()-startOpt;
prev.setTime(next.getTime());
if (optResult!=origResult) {
throw new RuntimeException ("Ouch");
}
}
Window.alert(count+" steps took \noptimized code: "+optTime+"\noriginal code: "+origTime+"ms");
// GWT.log ("Optimized implementation> "+count+" steps took "+optTime+"ms");
// GWT.log ("Original implementation> "+count+" steps took "+origTime+"ms");
}
/**
* {@code CalendarUtil.getDaysBetween()} optimized reimplementation.
*
* @see CalendarUtil#getDaysBetween(Date, Date)
*/
protected int getDaysBetween(final Date start, final Date finish) {
// Extracts midnight time for both dates
final long aTime = (start.getTime() / 1000) * 1000;
final long bTime = (finish.getTime() / 1000) * 1000;
long adjust = 60 * 60 * 1000;
adjust = (bTime > aTime) ? adjust : -adjust;
return (int) ((bTime - aTime + adjust) / (24 * 60 * 60 * 1000));
}
}