KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > example > time > DateTimePerformance


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.example.time;
17
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.GregorianCalendar JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.joda.time.DateTime;
28 import org.joda.time.MutableDateTime;
29 import org.joda.time.chrono.GJChronology;
30 import org.joda.time.format.DateTimeFormat;
31 import org.joda.time.format.DateTimeFormatter;
32
33 /**
34  * DateTimePerformance provides various comparisons between the Java supplied
35  * Date classes and the Joda ones.
36  *
37  * @author Stephen Colebourne
38  */

39 public class DateTimePerformance {
40     
41     private static class Result {
42         String JavaDoc object = null;
43         String JavaDoc name = null;
44         long time = 0;
45         long avg = 0;
46         int runs = 0;
47     }
48     
49     private static int AVERAGE = 3;
50     private static int COUNT_VERY_FAST = 5000000;
51     private static int COUNT_FAST = 200000;
52     private static int COUNT_SLOW = 20000;
53     
54     private Map JavaDoc results = new HashMap JavaDoc();
55     private List JavaDoc resultList = new ArrayList JavaDoc();
56     
57     private Result result = null;
58     private long start = 0;
59     private long end = 0;
60     
61     /**
62      * Constructor
63      */

64     public static void main(String JavaDoc[] args) {
65         try {
66             new DateTimePerformance();
67             
68         } catch (Throwable JavaDoc th) {
69             th.printStackTrace();
70         }
71     }
72     
73     /**
74      * Constructor
75      */

76     public DateTimePerformance() throws Exception JavaDoc {
77         checkJodaConstructor1();
78         checkJISOConstructor1();
79         checkGCalConstructor1();
80         checkDateConstructor1();
81         
82         checkJodaConstructor2();
83         checkJISOConstructor2();
84         checkGCalConstructor2();
85         checkDateConstructor2();
86         
87         checkJodaConstructor3();
88         checkJISOConstructor3();
89         checkGCalConstructor3();
90         checkDateConstructor3();
91         
92         checkJodaGetYear();
93         checkJISOGetYear();
94         checkGCalGetYear();
95         checkDateGetYear();
96         
97 // checkJodaGetMonth();
98
// checkJISOGetMonth();
99
// checkGCalGetMonth();
100
// checkDateGetMonth();
101

102 // checkJodaGetDay();
103
// checkJISOGetDay();
104
// checkGCalGetDay();
105
// checkDateGetDay();
106

107         checkJodaGetHour();
108         checkJISOGetHour();
109         checkGCalGetHour();
110         checkDateGetHour();
111         
112         checkJodaSetYear();
113         checkJISOSetYear();
114         checkGCalSetYear();
115         checkDateSetYear();
116         
117         checkJodaSetGetYear();
118         checkJISOSetGetYear();
119         checkGCalSetGetYear();
120         checkDateSetGetYear();
121         
122         checkJodaSetHour();
123         checkJISOSetHour();
124         checkGCalSetHour();
125         checkDateSetHour();
126         
127         checkJodaSetGetHour();
128         checkJISOSetGetHour();
129         checkGCalSetGetHour();
130         checkDateSetGetHour();
131         
132         checkJodaToString();
133         checkJISOToString();
134         checkGCalToString();
135         checkDateToString();
136         
137         System.out.println("");
138         long jodaTotal = 0;
139         long jisoTotal = 0;
140         long gcalTotal = 0;
141         long dateTotal = 0;
142         for (Iterator JavaDoc it = resultList.iterator(); it.hasNext();) {
143             Result res = (Result) it.next();
144             System.out.println(res.object + "." + res.name + ": " + res.avg + "ns");
145             if (res.object.equals("Joda")) {
146                 jodaTotal += res.avg;
147             } else if (res.object.equals("JISO")) {
148                 jisoTotal += res.avg;
149             } else if (res.object.equals("GCal")) {
150                 gcalTotal += res.avg;
151             } else if (res.object.equals("Date")) {
152                 dateTotal += res.avg;
153                 System.out.println("");
154             }
155         }
156         System.out.println("Joda: " + jodaTotal);
157         System.out.println("JISO: " + jisoTotal);
158         System.out.println("GCal: " + gcalTotal);
159         System.out.println("Date: " + dateTotal);
160     }
161
162     // Constructor using currentTimeMillis()
163
//------------------------------------------------------------------------
164

165     private void checkJodaConstructor1() {
166         int COUNT = COUNT_SLOW;
167         DateTime dt = new DateTime(GJChronology.getInstance());
168         int count = 0;
169         for (int i = 0; i < AVERAGE; i++) {
170             start("Joda", "new()");
171             for (int j = 0; j < COUNT; j++) {
172                 dt = new DateTime(GJChronology.getInstance());
173                 if (count++ < 0) {System.out.println("Anti optimise");}
174             }
175             end(COUNT);
176         }
177     }
178
179     private void checkJISOConstructor1() {
180         int COUNT = COUNT_SLOW;
181         DateTime dt = new DateTime();
182         int count = 0;
183         for (int i = 0; i < AVERAGE; i++) {
184             start("JISO", "new()");
185             for (int j = 0; j < COUNT; j++) {
186                 dt = new DateTime();
187                 if (count++ < 0) {System.out.println("Anti optimise");}
188             }
189             end(COUNT);
190         }
191     }
192
193     private void checkGCalConstructor1() {
194         int COUNT = COUNT_SLOW;
195         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
196         int count = 0;
197         for (int i = 0; i < AVERAGE; i++) {
198             start("GCal", "new()");
199             for (int j = 0; j < COUNT; j++) {
200                 dt = new GregorianCalendar JavaDoc();
201                 if (count++ < 0) {System.out.println("Anti optimise");}
202             }
203             end(COUNT);
204         }
205     }
206
207     private void checkDateConstructor1() {
208         int COUNT = COUNT_SLOW;
209         Date JavaDoc dt = new Date JavaDoc();
210         int count = 0;
211         for (int i = 0; i < AVERAGE; i++) {
212             start("Date", "new()");
213             for (int j = 0; j < COUNT; j++) {
214                 dt = new Date JavaDoc();
215                 if (count++ < 0) {System.out.println("Anti optimise");}
216             }
217             end(COUNT);
218         }
219     }
220
221     // Constructor using long millis
222
//------------------------------------------------------------------------
223

224     private void checkJodaConstructor2() {
225         int COUNT = COUNT_VERY_FAST;
226         DateTime dt = new DateTime(12345L, GJChronology.getInstance());
227         for (int i = 0; i < AVERAGE; i++) {
228             start("Joda", "new(millis)");
229             for (int j = 0; j < COUNT; j++) {
230                 dt = new DateTime(12345L, GJChronology.getInstance());
231                 if (dt == null) {System.out.println("Anti optimise");}
232             }
233             end(COUNT);
234         }
235     }
236
237     private void checkJISOConstructor2() {
238         int COUNT = COUNT_VERY_FAST;
239         DateTime dt = new DateTime(12345L);
240         for (int i = 0; i < AVERAGE; i++) {
241             start("JISO", "new(millis)");
242             for (int j = 0; j < COUNT; j++) {
243                 dt = new DateTime(12345L);
244                 if (dt == null) {System.out.println("Anti optimise");}
245             }
246             end(COUNT);
247         }
248     }
249
250     private void checkGCalConstructor2() {
251         int COUNT = COUNT_SLOW;
252         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
253         for (int i = 0; i < AVERAGE; i++) {
254             start("GCal", "new(millis)");
255             for (int j = 0; j < COUNT; j++) {
256                 dt = new GregorianCalendar JavaDoc();
257                 dt.setTime(new Date JavaDoc(12345L));
258                 if (dt == null) {System.out.println("Anti optimise");}
259             }
260             end(COUNT);
261         }
262     }
263
264     private void checkDateConstructor2() {
265         int COUNT = COUNT_VERY_FAST;
266         Date JavaDoc dt = new Date JavaDoc();
267         for (int i = 0; i < AVERAGE; i++) {
268             start("Date", "new(millis)");
269             for (int j = 0; j < COUNT; j++) {
270                 dt = new Date JavaDoc(12345L);
271                 if (dt == null) {System.out.println("Anti optimise");}
272             }
273             end(COUNT);
274         }
275     }
276
277     // Constructor using year month and day
278
//------------------------------------------------------------------------
279

280     private void checkJodaConstructor3() {
281         int COUNT = COUNT_SLOW;
282         DateTime dt = new DateTime(1972, 10, 1, 0, 0, 0, 0,
283                                    GJChronology.getInstance());
284         for (int i = 0; i < AVERAGE; i++) {
285             start("Joda", "new(YMD)");
286             for (int j = 0; j < COUNT; j++) {
287                 dt = new DateTime(1972, 10, 1, 0, 0, 0, 0,
288                                   GJChronology.getInstance());
289                 if (dt == null) {System.out.println("Anti optimise");}
290             }
291             end(COUNT);
292         }
293     }
294
295     private void checkJISOConstructor3() {
296         int COUNT = COUNT_SLOW;
297         DateTime dt = new DateTime(1972, 10, 1, 0, 0, 0, 0);
298         for (int i = 0; i < AVERAGE; i++) {
299             start("JISO", "new(YMD)");
300             for (int j = 0; j < COUNT; j++) {
301                 dt = new DateTime(1972, 10, 1, 0, 0, 0, 0);
302                 if (dt == null) {System.out.println("Anti optimise");}
303             }
304             end(COUNT);
305         }
306     }
307
308     private void checkGCalConstructor3() {
309         int COUNT = COUNT_SLOW;
310         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc(1972, 10, 1);
311         for (int i = 0; i < AVERAGE; i++) {
312             start("GCal", "new(YMD)");
313             for (int j = 0; j < COUNT; j++) {
314                 dt = new GregorianCalendar JavaDoc(1972, 10, 1);
315                 if (dt == null) {System.out.println("Anti optimise");}
316             }
317             end(COUNT);
318         }
319     }
320
321     private void checkDateConstructor3() {
322         int COUNT = COUNT_SLOW;
323         Date JavaDoc dt = new Date JavaDoc();
324         for (int i = 0; i < AVERAGE; i++) {
325             start("Date", "new(YMD)");
326             for (int j = 0; j < COUNT; j++) {
327                 dt = new Date JavaDoc(1972, 10, 1);
328                 if (dt == null) {System.out.println("Anti optimise");}
329             }
330             end(COUNT);
331         }
332     }
333
334     // Get year
335
//------------------------------------------------------------------------
336

337     private void checkJodaGetYear() {
338         int COUNT = COUNT_VERY_FAST;
339         DateTime dt = new DateTime(GJChronology.getInstance());
340         for (int i = 0; i < AVERAGE; i++) {
341             start("Joda", "getYear");
342             for (int j = 0; j < COUNT; j++) {
343                 int val = dt.getYear();
344                 if (val == 0) {System.out.println("Anti optimise");}
345             }
346             end(COUNT);
347         }
348     }
349
350     private void checkJISOGetYear() {
351         int COUNT = COUNT_VERY_FAST;
352         DateTime dt = new DateTime();
353         for (int i = 0; i < AVERAGE; i++) {
354             start("JISO", "getYear");
355             for (int j = 0; j < COUNT; j++) {
356                 int val = dt.getYear();
357                 if (val == 0) {System.out.println("Anti optimise");}
358             }
359             end(COUNT);
360         }
361     }
362
363     private void checkGCalGetYear() {
364         int COUNT = COUNT_VERY_FAST;
365         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
366         for (int i = 0; i < AVERAGE; i++) {
367             start("GCal", "getYear");
368             for (int j = 0; j < COUNT; j++) {
369                 int val = dt.get(GregorianCalendar.YEAR);
370                 if (val == 0) {System.out.println("Anti optimise");}
371             }
372             end(COUNT);
373         }
374     }
375
376     private void checkDateGetYear() {
377         int COUNT = COUNT_FAST;
378         Date JavaDoc dt = new Date JavaDoc();
379         for (int i = 0; i < AVERAGE; i++) {
380             start("Date", "getYear");
381             for (int j = 0; j < COUNT; j++) {
382                 int val = dt.getYear();
383                 if (val == 0) {System.out.println("Anti optimise");}
384             }
385             end(COUNT);
386         }
387     }
388
389     // Get month
390
//------------------------------------------------------------------------
391

392     private void checkJodaGetMonth() {
393         int COUNT = COUNT_VERY_FAST;
394         DateTime dt = new DateTime(GJChronology.getInstance());
395         for (int i = 0; i < AVERAGE; i++) {
396             start("Joda", "getMonth");
397             for (int j = 0; j < COUNT; j++) {
398                 int val = dt.getMonthOfYear();
399                 if (val == 0) {System.out.println("Anti optimise");}
400             }
401             end(COUNT);
402         }
403     }
404
405     private void checkJISOGetMonth() {
406         int COUNT = COUNT_VERY_FAST;
407         DateTime dt = new DateTime();
408         for (int i = 0; i < AVERAGE; i++) {
409             start("JISO", "getMonth");
410             for (int j = 0; j < COUNT; j++) {
411                 int val = dt.getMonthOfYear();
412                 if (val == 0) {System.out.println("Anti optimise");}
413             }
414             end(COUNT);
415         }
416     }
417
418     private void checkGCalGetMonth() {
419         int COUNT = COUNT_VERY_FAST;
420         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
421         for (int i = 0; i < AVERAGE; i++) {
422             start("GCal", "getMonth");
423             for (int j = 0; j < COUNT; j++) {
424                 int val = dt.get(GregorianCalendar.MONTH);
425                 if (val == 0) {System.out.println("Anti optimise");}
426             }
427             end(COUNT);
428         }
429     }
430
431     private void checkDateGetMonth() {
432         int COUNT = COUNT_FAST;
433         Date JavaDoc dt = new Date JavaDoc();
434         for (int i = 0; i < AVERAGE; i++) {
435             start("Date", "getMonth");
436             for (int j = 0; j < COUNT; j++) {
437                 int val = dt.getMonth();
438                 if (val == 0) {System.out.println("Anti optimise");}
439             }
440             end(COUNT);
441         }
442     }
443
444     // Get day
445
//------------------------------------------------------------------------
446

447     private void checkJodaGetDay() {
448         int COUNT = COUNT_VERY_FAST;
449         DateTime dt = new DateTime(GJChronology.getInstance());
450         for (int i = 0; i < AVERAGE; i++) {
451             start("Joda", "getDay");
452             for (int j = 0; j < COUNT; j++) {
453                 int val = dt.getDayOfMonth();
454                 if (val == 0) {System.out.println("Anti optimise");}
455             }
456             end(COUNT);
457         }
458     }
459
460     private void checkJISOGetDay() {
461         int COUNT = COUNT_VERY_FAST;
462         DateTime dt = new DateTime();
463         for (int i = 0; i < AVERAGE; i++) {
464             start("JISO", "getDay");
465             for (int j = 0; j < COUNT; j++) {
466                 int val = dt.getDayOfMonth();
467                 if (val == 0) {System.out.println("Anti optimise");}
468             }
469             end(COUNT);
470         }
471     }
472
473     private void checkGCalGetDay() {
474         int COUNT = COUNT_VERY_FAST;
475         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
476         for (int i = 0; i < AVERAGE; i++) {
477             start("GCal", "getDay");
478             for (int j = 0; j < COUNT; j++) {
479                 int val = dt.get(GregorianCalendar.DAY_OF_MONTH);
480                 if (val == 0) {System.out.println("Anti optimise");}
481             }
482             end(COUNT);
483         }
484     }
485
486     private void checkDateGetDay() {
487         int COUNT = COUNT_FAST;
488         Date JavaDoc dt = new Date JavaDoc();
489         for (int i = 0; i < AVERAGE; i++) {
490             start("Date", "getDay");
491             for (int j = 0; j < COUNT; j++) {
492                 int val = dt.getDate();
493                 if (val == 0) {System.out.println("Anti optimise");}
494             }
495             end(COUNT);
496         }
497     }
498
499     // Get hour
500
//------------------------------------------------------------------------
501

502     private void checkJodaGetHour() {
503         int COUNT = COUNT_VERY_FAST;
504         DateTime dt = new DateTime(GJChronology.getInstance());
505         for (int i = 0; i < AVERAGE; i++) {
506             start("Joda", "getHour");
507             for (int j = 0; j < COUNT; j++) {
508                 int val = dt.getHourOfDay();
509                 if (val == -1) {System.out.println("Anti optimise");}
510             }
511             end(COUNT);
512         }
513     }
514
515     private void checkJISOGetHour() {
516         int COUNT = COUNT_VERY_FAST;
517         DateTime dt = new DateTime();
518         for (int i = 0; i < AVERAGE; i++) {
519             start("JISO", "getHour");
520             for (int j = 0; j < COUNT; j++) {
521                 int val = dt.getHourOfDay();
522                 if (val == -1) {System.out.println("Anti optimise");}
523             }
524             end(COUNT);
525         }
526     }
527
528     private void checkGCalGetHour() {
529         int COUNT = COUNT_VERY_FAST;
530         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
531         for (int i = 0; i < AVERAGE; i++) {
532             start("GCal", "getHour");
533             for (int j = 0; j < COUNT; j++) {
534                 int val = dt.get(GregorianCalendar.HOUR_OF_DAY);
535                 if (val == -1) {System.out.println("Anti optimise");}
536             }
537             end(COUNT);
538         }
539     }
540
541     private void checkDateGetHour() {
542         int COUNT = COUNT_FAST;
543         Date JavaDoc dt = new Date JavaDoc();
544         for (int i = 0; i < AVERAGE; i++) {
545             start("Date", "getHour");
546             for (int j = 0; j < COUNT; j++) {
547                 int val = dt.getHours();
548                 if (val == -1) {System.out.println("Anti optimise");}
549             }
550             end(COUNT);
551         }
552     }
553
554     // Set year
555
//------------------------------------------------------------------------
556

557     private void checkJodaSetYear() {
558         int COUNT = COUNT_FAST;
559         // Is it fair to use only MutableDateTime here? You decide.
560
MutableDateTime dt = new MutableDateTime(GJChronology.getInstance());
561         for (int i = 0; i < AVERAGE; i++) {
562             start("Joda", "setYear");
563             for (int j = 0; j < COUNT; j++) {
564                 dt.setYear(1972);
565                 if (dt == null) {System.out.println("Anti optimise");}
566             }
567             end(COUNT);
568         }
569     }
570
571     private void checkJISOSetYear() {
572         int COUNT = COUNT_FAST;
573         // Is it fair to use only MutableDateTime here? You decide.
574
MutableDateTime dt = new MutableDateTime();
575         for (int i = 0; i < AVERAGE; i++) {
576             start("JISO", "setYear");
577             for (int j = 0; j < COUNT; j++) {
578                 dt.setYear(1972);
579                 if (dt == null) {System.out.println("Anti optimise");}
580             }
581             end(COUNT);
582         }
583     }
584
585     private void checkGCalSetYear() {
586         int COUNT = COUNT_VERY_FAST;
587         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
588         for (int i = 0; i < AVERAGE; i++) {
589             start("GCal", "setYear");
590             for (int j = 0; j < COUNT; j++) {
591                 dt.set(GregorianCalendar.YEAR, 1972);
592                 if (dt == null) {System.out.println("Anti optimise");}
593             }
594             end(COUNT);
595         }
596     }
597
598     private void checkDateSetYear() {
599         int COUNT = COUNT_FAST;
600         Date JavaDoc dt = new Date JavaDoc();
601         for (int i = 0; i < AVERAGE; i++) {
602             start("Date", "setYear");
603             for (int j = 0; j < COUNT; j++) {
604                 dt.setYear(1972);
605                 if (dt == null) {System.out.println("Anti optimise");}
606             }
607             end(COUNT);
608         }
609     }
610
611     // Set then get year
612
//------------------------------------------------------------------------
613

614     private void checkJodaSetGetYear() {
615         int COUNT = COUNT_FAST;
616         // Is it fair to use only MutableDateTime here? You decide.
617
// MutableDateTime dt = new MutableDateTime(GJChronology.getInstance());
618
// for (int i = 0; i < AVERAGE; i++) {
619
// start("Joda", "setGetYear");
620
// for (int j = 0; j < COUNT; j++) {
621
// dt.setYear(1972);
622
// int val = dt.getYear();
623
// if (val < 0) {System.out.println("Anti optimise");}
624
// }
625
// end(COUNT);
626
// }
627
DateTime dt = new DateTime(GJChronology.getInstance());
628         for (int i = 0; i < AVERAGE; i++) {
629             start("Joda", "setGetYear");
630             for (int j = 0; j < COUNT; j++) {
631                 dt = dt.year().setCopy(1972);
632                 int val = dt.getYear();
633                 if (val < 0) {System.out.println("Anti optimise");}
634             }
635             end(COUNT);
636         }
637     }
638
639     private void checkJISOSetGetYear() {
640         int COUNT = COUNT_FAST;
641         // Is it fair to use only MutableDateTime here? You decide.
642
// MutableDateTime dt = new MutableDateTime();
643
// for (int i = 0; i < AVERAGE; i++) {
644
// start("JISO", "setGetYear");
645
// for (int j = 0; j < COUNT; j++) {
646
// dt.setYear(1972);
647
// int val = dt.getYear();
648
// if (val < 0) {System.out.println("Anti optimise");}
649
// }
650
// end(COUNT);
651
// }
652
DateTime dt = new DateTime();
653         for (int i = 0; i < AVERAGE; i++) {
654             start("JISO", "setGetYear");
655             for (int j = 0; j < COUNT; j++) {
656                 dt = dt.year().setCopy(1972);
657                 int val = dt.getYear();
658                 if (val < 0) {System.out.println("Anti optimise");}
659             }
660             end(COUNT);
661         }
662     }
663
664     private void checkGCalSetGetYear() {
665         int COUNT = COUNT_FAST;
666         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
667         for (int i = 0; i < AVERAGE; i++) {
668             start("GCal", "setGetYear");
669             for (int j = 0; j < COUNT; j++) {
670                 dt.set(GregorianCalendar.YEAR, 1972);
671                 int val = dt.get(GregorianCalendar.YEAR);
672                 if (val < 0) {System.out.println("Anti optimise");}
673             }
674             end(COUNT);
675         }
676     }
677
678     private void checkDateSetGetYear() {
679         int COUNT = COUNT_FAST;
680         Date JavaDoc dt = new Date JavaDoc();
681         for (int i = 0; i < AVERAGE; i++) {
682             start("Date", "setGetYear");
683             for (int j = 0; j < COUNT; j++) {
684                 dt.setYear(1972);
685                 int val = dt.getYear();
686                 if (val < 0) {System.out.println("Anti optimise");}
687             }
688             end(COUNT);
689         }
690     }
691
692     // Set hour
693
//------------------------------------------------------------------------
694

695     private void checkJodaSetHour() {
696         int COUNT = COUNT_VERY_FAST;
697         // Is it fair to use only MutableDateTime here? You decide.
698
MutableDateTime dt = new MutableDateTime(GJChronology.getInstance());
699         for (int i = 0; i < AVERAGE; i++) {
700             start("Joda", "setHour");
701             for (int j = 0; j < COUNT; j++) {
702                 dt.setHourOfDay(13);
703                 if (dt == null) {System.out.println("Anti optimise");}
704             }
705             end(COUNT);
706         }
707     }
708
709     private void checkJISOSetHour() {
710         int COUNT = COUNT_VERY_FAST;
711         // Is it fair to use only MutableDateTime here? You decide.
712
MutableDateTime dt = new MutableDateTime();
713         for (int i = 0; i < AVERAGE; i++) {
714             start("JISO", "setHour");
715             for (int j = 0; j < COUNT; j++) {
716                 dt.setHourOfDay(13);
717                 if (dt == null) {System.out.println("Anti optimise");}
718             }
719             end(COUNT);
720         }
721     }
722
723     private void checkGCalSetHour() {
724         int COUNT = COUNT_VERY_FAST;
725         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
726         for (int i = 0; i < AVERAGE; i++) {
727             start("GCal", "setHour");
728             for (int j = 0; j < COUNT; j++) {
729                 dt.set(GregorianCalendar.HOUR_OF_DAY, 13);
730                 if (dt == null) {System.out.println("Anti optimise");}
731             }
732             end(COUNT);
733         }
734     }
735
736     private void checkDateSetHour() {
737         int COUNT = COUNT_FAST;
738         Date JavaDoc dt = new Date JavaDoc();
739         for (int i = 0; i < AVERAGE; i++) {
740             start("Date", "setHour");
741             for (int j = 0; j < COUNT; j++) {
742                 dt.setHours(13);
743                 if (dt == null) {System.out.println("Anti optimise");}
744             }
745             end(COUNT);
746         }
747     }
748
749     // Set hour
750
//------------------------------------------------------------------------
751

752     private void checkJodaSetGetHour() {
753         int COUNT = COUNT_VERY_FAST;
754         // Is it fair to use only MutableDateTime here? You decide.
755
MutableDateTime dt = new MutableDateTime(GJChronology.getInstance());
756         for (int i = 0; i < AVERAGE; i++) {
757             start("Joda", "setGetHour");
758             for (int j = 0; j < COUNT; j++) {
759                 dt.setHourOfDay(13);
760                 int val = dt.getHourOfDay();
761                 if (dt == null) {System.out.println("Anti optimise");}
762             }
763             end(COUNT);
764         }
765     }
766
767     private void checkJISOSetGetHour() {
768         int COUNT = COUNT_VERY_FAST;
769         // Is it fair to use only MutableDateTime here? You decide.
770
MutableDateTime dt = new MutableDateTime();
771         for (int i = 0; i < AVERAGE; i++) {
772             start("JISO", "setGetHour");
773             for (int j = 0; j < COUNT; j++) {
774                 dt.setHourOfDay(13);
775                 int val = dt.getHourOfDay();
776                 if (dt == null) {System.out.println("Anti optimise");}
777             }
778             end(COUNT);
779         }
780     }
781
782     private void checkGCalSetGetHour() {
783         int COUNT = COUNT_VERY_FAST;
784         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
785         for (int i = 0; i < AVERAGE; i++) {
786             start("GCal", "setGetHour");
787             for (int j = 0; j < COUNT; j++) {
788                 dt.set(GregorianCalendar.HOUR_OF_DAY, 13);
789                 int val = dt.get(GregorianCalendar.HOUR_OF_DAY);
790                 if (dt == null) {System.out.println("Anti optimise");}
791             }
792             end(COUNT);
793         }
794     }
795
796     private void checkDateSetGetHour() {
797         int COUNT = COUNT_FAST;
798         Date JavaDoc dt = new Date JavaDoc();
799         for (int i = 0; i < AVERAGE; i++) {
800             start("Date", "setGetHour");
801             for (int j = 0; j < COUNT; j++) {
802                 dt.setHours(13);
803                 int val = dt.getHours();
804                 if (dt == null) {System.out.println("Anti optimise");}
805             }
806             end(COUNT);
807         }
808     }
809
810     // To formatted string
811
//------------------------------------------------------------------------
812

813     private void checkJodaToString() {
814         int COUNT = COUNT_SLOW;
815         DateTime dt = new DateTime(GJChronology.getInstance());
816         DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy");
817         for (int i = 0; i < AVERAGE; i++) {
818             start("Joda", "toString");
819             for (int j = 0; j < COUNT; j++) {
820                 String JavaDoc str = dt.toString("dd MMM yyyy");
821 // String str = dt.toString(f);
822
if (str == null) {System.out.println("Anti optimise");}
823             }
824             end(COUNT);
825         }
826     }
827
828     private void checkJISOToString() {
829         int COUNT = COUNT_SLOW;
830         DateTime dt = new DateTime();
831         DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy");
832         for (int i = 0; i < AVERAGE; i++) {
833             start("JISO", "toString");
834             for (int j = 0; j < COUNT; j++) {
835                 String JavaDoc str = dt.toString("dd MMM yyyy");
836 // String str = dt.toString(f);
837
if (str == null) {System.out.println("Anti optimise");}
838             }
839             end(COUNT);
840         }
841     }
842
843     private void checkGCalToString() {
844         int COUNT = COUNT_SLOW;
845         GregorianCalendar JavaDoc dt = new GregorianCalendar JavaDoc();
846         for (int i = 0; i < AVERAGE; i++) {
847             start("GCal", "toString");
848             for (int j = 0; j < COUNT; j++) {
849                 SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("dd MMM yyyy");
850                 String JavaDoc str = sdf.format(dt.getTime());
851                 if (str == null) {System.out.println("Anti optimise");}
852             }
853             end(COUNT);
854         }
855     }
856
857     private void checkDateToString() {
858         int COUNT = COUNT_SLOW;
859         Date JavaDoc dt = new Date JavaDoc();
860         for (int i = 0; i < AVERAGE; i++) {
861             start("Date", "toString");
862             for (int j = 0; j < COUNT; j++) {
863                 SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("dd MMM yyyy");
864                 String JavaDoc str = sdf.format(dt);
865                 if (str == null) {System.out.println("Anti optimise");}
866             }
867             end(COUNT);
868         }
869     }
870
871     //------------------------------------------------------------------------
872

873     /**
874      * Start the stopwatch.
875      */

876     private void start(String JavaDoc str1, String JavaDoc str2) {
877         result = (Result) results.get(str1 + str2);
878         if (result == null) {
879             result = new Result();
880             result.object = str1;
881             result.name = str2;
882             results.put(str1 + str2, result);
883             resultList.add(result);
884         }
885         start = System.currentTimeMillis();
886     }
887    
888     /**
889      * End the stopwatch and print the result.
890      */

891     private void end(int count) {
892         end = System.currentTimeMillis();
893         long time = (end - start);
894         result.time = result.time + time;
895         result.runs = result.runs + count;
896         result.avg = (result.time * 1000000) / result.runs;
897         System.out.print(".");
898     }
899
900 }
901
Popular Tags