View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.issue.lbclassic36;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.Date;
18  
19  //import org.joda.time.format.DateTimeFormat;
20  //import org.joda.time.format.DateTimeFormatter;
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          // DateTimeFormatter jodaFormat = DateTimeFormat.forPattern(ISO8601_PATTERN);
44          @SuppressWarnings("unused")
45          long timeInMillis = new Date().getTime();
46          long start = System.nanoTime();
47          for (int i = 0; i < RUN_LENGTH; ++i) {
48              // jodaFormat.print(timeInMillis);
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  }