1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  package ch.qos.logback.classic.issue.lbclassic36;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.Date;
18  
19  
20  
21  
22  public class DateFormatPerf_Tapp {
23      public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
24      static final long NANOS_IN_ONE_SEC = 1000 * 1000 * 1000L;
25  
26      static long RUN_LENGTH = 1000 * 1000;
27  
28      public static void main(String[] args) {
29          for (int i = 0; i < 5; i++) {
30              doRawJoda();
31              doRawSDF();
32          }
33  
34          print("Raw Joda:     ", doRawJoda());
35          print("Raw SDF:      ", doRawSDF());
36      }
37  
38      static void print(String msg, double avg) {
39          System.out.println(msg + " average tick " + avg + " nanoseconds");
40      }
41  
42      static double doRawJoda() {
43          
44          @SuppressWarnings("unused")
45          long timeInMillis = new Date().getTime();
46          long start = System.nanoTime();
47          for (int i = 0; i < RUN_LENGTH; ++i) {
48              
49          }
50          return (System.nanoTime() - start) * 1.0d / RUN_LENGTH;
51      }
52  
53      static double doRawSDF() {
54          SimpleDateFormat simpleFormat = new SimpleDateFormat(ISO8601_PATTERN);
55          long timeInMillis = new Date().getTime();
56          long start = System.nanoTime();
57          for (int i = 0; i < RUN_LENGTH; ++i) {
58              simpleFormat.format(timeInMillis);
59          }
60          return (System.nanoTime() - start) * 1.0d / RUN_LENGTH;
61      }
62  
63  }