• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import java.text.NumberFormat;
35 import java.util.Calendar;
36 import java.util.Date;
37 import java.util.GregorianCalendar;
38 
39 import org.threeten.bp.zone.ZoneRules;
40 
41 /**
42  * Test Performance.
43  */
44 public class PerformanceZone {
45 
46     /** The year to test. */
47     private static final int YEAR = 1980;
48     /** Size. */
49     private static final NumberFormat NF = NumberFormat.getIntegerInstance();
50     static {
51         NF.setGroupingUsed(true);
52     }
53     /** Size. */
54     private static final int SIZE = 200000;
55 
56     /**
57      * Main.
58      * @param args  the arguments
59      */
main(String[] args)60     public static void main(String[] args) {
61         LocalTime time = LocalTime.of(12, 30, 20);
62         System.out.println(time);
63 
64         for (int i = 0; i < 6; i++) {
65             jsrLocalGetOffset();
66             jsrInstantGetOffset();
67             jsrRulesLocalGetOffset();
68             jsrRulesInstantGetOffset();
69             jdkLocalGetOffset();
70             jdkInstantGetOffset();
71             System.out.println();
72         }
73     }
74 
75     //-----------------------------------------------------------------------
jsrLocalGetOffset()76     private static void jsrLocalGetOffset() {
77         LocalDateTime dt = LocalDateTime.of(YEAR, 6, 1, 12, 0);
78         ZoneId tz = ZoneId.of("Europe/London");
79         ZoneOffset[] list = new ZoneOffset[SIZE];
80         long start = System.nanoTime();
81         for (int i = 0; i < SIZE; i++) {
82             list[i] = tz.getRules().getOffset(dt);
83         }
84         long end = System.nanoTime();
85         System.out.println("JSR-Loc: Setup:  " + NF.format(end - start) + " ns" + list[0]);
86     }
87 
88     //-----------------------------------------------------------------------
jsrInstantGetOffset()89     private static void jsrInstantGetOffset() {
90         Instant instant = LocalDateTime.of(YEAR, 6, 1, 12, 0).toInstant(ZoneOffset.ofHours(1));
91         ZoneId tz = ZoneId.of("Europe/London");
92         ZoneOffset[] list = new ZoneOffset[SIZE];
93         long start = System.nanoTime();
94         for (int i = 0; i < SIZE; i++) {
95             list[i] = tz.getRules().getOffset(instant);
96         }
97         long end = System.nanoTime();
98         System.out.println("JSR-Ins: Setup:  " + NF.format(end - start) + " ns" + list[0]);
99     }
100 
101     //-----------------------------------------------------------------------
jsrRulesLocalGetOffset()102     private static void jsrRulesLocalGetOffset() {
103         LocalDateTime dt = LocalDateTime.of(YEAR, 6, 1, 12, 0);
104         ZoneRules tz = ZoneId.of("Europe/London").getRules();
105         ZoneOffset[] list = new ZoneOffset[SIZE];
106         long start = System.nanoTime();
107         for (int i = 0; i < SIZE; i++) {
108             list[i] = tz.getOffset(dt);
109         }
110         long end = System.nanoTime();
111         System.out.println("JSR-LoR: Setup:  " + NF.format(end - start) + " ns" + list[0]);
112     }
113 
114     //-----------------------------------------------------------------------
jsrRulesInstantGetOffset()115     private static void jsrRulesInstantGetOffset() {
116         Instant instant = LocalDateTime.of(YEAR, 6, 1, 12, 0).toInstant(ZoneOffset.ofHours(1));
117         ZoneRules tz = ZoneId.of("Europe/London").getRules();
118         ZoneOffset[] list = new ZoneOffset[SIZE];
119         long start = System.nanoTime();
120         for (int i = 0; i < SIZE; i++) {
121             list[i] = tz.getOffset(instant);
122         }
123         long end = System.nanoTime();
124         System.out.println("JSR-InR: Setup:  " + NF.format(end - start) + " ns" + list[0]);
125     }
126 
127     //-----------------------------------------------------------------------
jdkLocalGetOffset()128     private static void jdkLocalGetOffset() {
129         java.util.TimeZone tz = java.util.TimeZone.getTimeZone("Europe/London");
130         int[] list = new int[SIZE];
131         long start = System.nanoTime();
132         for (int i = 0; i < SIZE; i++) {
133             list[i] = tz.getOffset(GregorianCalendar.AD, YEAR, 0, 11, Calendar.SUNDAY, 0);
134         }
135         long end = System.nanoTime();
136         System.out.println("GCalLoc: Setup:  " + NF.format(end - start) + " ns" + list[0]);
137     }
138 
139     //-----------------------------------------------------------------------
jdkInstantGetOffset()140     private static void jdkInstantGetOffset() {
141         java.util.TimeZone tz = java.util.TimeZone.getTimeZone("Europe/London");
142         GregorianCalendar dt = new GregorianCalendar(tz);
143         dt.setGregorianChange(new Date(Long.MIN_VALUE));
144         dt.set(YEAR, 5, 1, 12, 0);
145         int[] list = new int[SIZE];
146         long start = System.nanoTime();
147         for (int i = 0; i < SIZE; i++) {
148             list[i] = tz.getOffset(dt.getTimeInMillis());
149         }
150         long end = System.nanoTime();
151         System.out.println("GCalIns: Setup:  " + NF.format(end - start) + " ns" + list[0]);
152     }
153 
154 }
155