1+ /*
2+ * Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation.
8+ *
9+ * This code is distributed in the hope that it will be useful, but WITHOUT
10+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+ * version 2 for more details (a copy is included in the LICENSE file that
13+ * accompanied this code).
14+ *
15+ * You should have received a copy of the GNU General Public License version
16+ * 2 along with this work; if not, write to the Free Software Foundation,
17+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+ *
19+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+ * or visit www.oracle.com if you need additional information or have any
21+ * questions.
22+ */
23+ package org .openjdk .bench .java .time ;
24+
25+ import java .time .Duration ;
26+ import java .time .Instant ;
27+ import java .time .LocalDate ;
28+ import java .time .LocalDateTime ;
29+ import java .time .LocalTime ;
30+ import java .time .ZonedDateTime ;
31+ import java .time .ZoneOffset ;
32+ import java .time .format .DateTimeFormatter ;
33+ import java .time .temporal .ChronoUnit ;
34+
35+ import java .util .Locale ;
36+ import java .util .Random ;
37+ import java .util .concurrent .TimeUnit ;
38+ import java .util .stream .IntStream ;
39+ import java .util .stream .Stream ;
40+
41+ import org .openjdk .jmh .annotations .Benchmark ;
42+ import org .openjdk .jmh .annotations .BenchmarkMode ;
43+ import org .openjdk .jmh .annotations .Fork ;
44+ import org .openjdk .jmh .annotations .Measurement ;
45+ import org .openjdk .jmh .annotations .Mode ;
46+ import org .openjdk .jmh .annotations .OutputTimeUnit ;
47+ import org .openjdk .jmh .annotations .Param ;
48+ import org .openjdk .jmh .annotations .Scope ;
49+ import org .openjdk .jmh .annotations .Setup ;
50+ import org .openjdk .jmh .annotations .State ;
51+ import org .openjdk .jmh .annotations .Warmup ;
52+ import org .openjdk .jmh .infra .Blackhole ;
53+
54+ @ BenchmarkMode (Mode .Throughput )
55+ @ OutputTimeUnit (TimeUnit .MILLISECONDS )
56+ @ Warmup (iterations = 5 , time = 1 )
57+ @ Measurement (iterations = 5 , time = 1 )
58+ @ Fork (3 )
59+ @ State (Scope .Thread )
60+ public class ToStringBench {
61+ private static final Instant [] INSTANTS ;
62+ private static final ZonedDateTime [] ZONED_DATE_TIMES ;
63+ private static final LocalDateTime [] LOCAL_DATE_TIMES ;
64+ private static final LocalDate [] LOCAL_DATES ;
65+ private static final LocalTime [] LOCAL_TIMES ;
66+
67+ static {
68+ Instant loInstant = Instant .EPOCH .plus (Duration .ofDays (365 *50 )); // 2020-01-01
69+ Instant hiInstant = loInstant .plus (Duration .ofDays (1 ));
70+ long maxOffsetNanos = Duration .between (loInstant , hiInstant ).toNanos ();
71+ Random random = new Random (0 );
72+ INSTANTS = IntStream
73+ .range (0 , 1_000 )
74+ .mapToObj (ignored -> {
75+ final long offsetNanos = (long ) Math .floor (random .nextDouble () * maxOffsetNanos );
76+ return loInstant .plus (offsetNanos , ChronoUnit .NANOS );
77+ })
78+ .toArray (Instant []::new );
79+
80+ ZONED_DATE_TIMES = Stream .of (INSTANTS )
81+ .map (instant -> ZonedDateTime .ofInstant (instant , ZoneOffset .UTC ))
82+ .toArray (ZonedDateTime []::new );
83+
84+ LOCAL_DATE_TIMES = Stream .of (ZONED_DATE_TIMES )
85+ .map (zdt -> zdt .toLocalDateTime ())
86+ .toArray (LocalDateTime []::new );
87+
88+ LOCAL_DATES = Stream .of (LOCAL_DATE_TIMES )
89+ .map (ldt -> ldt .toLocalDate ())
90+ .toArray (LocalDate []::new );
91+
92+ LOCAL_TIMES = Stream .of (LOCAL_DATE_TIMES )
93+ .map (ldt -> ldt .toLocalTime ())
94+ .toArray (LocalTime []::new );
95+ }
96+
97+ @ Benchmark
98+ public void zonedDateTimeToString (Blackhole bh ) {
99+ for (final ZonedDateTime zonedDateTime : ZONED_DATE_TIMES ) {
100+ bh .consume (zonedDateTime .toString ());
101+ }
102+ }
103+
104+ @ Benchmark
105+ public void localDateTimeToString (Blackhole bh ) {
106+ for (LocalDateTime localDateTime : LOCAL_DATE_TIMES ) {
107+ bh .consume (localDateTime .toString ());
108+ }
109+ }
110+
111+ @ Benchmark
112+ public void localDateToString (Blackhole bh ) {
113+ for (LocalDate localDate : LOCAL_DATES ) {
114+ bh .consume (localDate .toString ());
115+ }
116+ }
117+
118+ @ Benchmark
119+ public void localTimeToString (Blackhole bh ) {
120+ for (LocalTime localTime : LOCAL_TIMES ) {
121+ bh .consume (localTime .toString ());
122+ }
123+ }
124+ }
0 commit comments