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 static org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH; 35 import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY; 36 import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR; 37 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR; 38 import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND; 39 import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE; 40 import static org.threeten.bp.temporal.ChronoField.YEAR; 41 42 import java.math.BigDecimal; 43 import java.text.NumberFormat; 44 import java.text.SimpleDateFormat; 45 import java.util.ArrayList; 46 import java.util.Arrays; 47 import java.util.Calendar; 48 import java.util.Collections; 49 import java.util.Date; 50 import java.util.GregorianCalendar; 51 import java.util.List; 52 import java.util.Locale; 53 import java.util.Map; 54 import java.util.Random; 55 import java.util.TreeMap; 56 57 import org.threeten.bp.format.DateTimeFormatter; 58 59 /** 60 * Test Performance. 61 */ 62 public class Performance { 63 64 /** Size. */ 65 private static final NumberFormat NF = NumberFormat.getIntegerInstance(); 66 static { 67 NF.setGroupingUsed(true); 68 } 69 /** Size. */ 70 private static final int SIZE = 100000; 71 /** Results. */ 72 private static final Map<String, long[]> RESULTS = new TreeMap<String, long[]>(); 73 /** Count. */ 74 private static int loop = 0; 75 76 /** 77 * Main. 78 * @param args the arguments 79 */ main(String[] args)80 public static void main(String[] args) { 81 for (loop = 0; loop < 5; loop++) { 82 System.out.println("-------------------------------------"); 83 process(); 84 } 85 86 System.out.println(); 87 for (String name : RESULTS.keySet()) { 88 System.out.println(name + " " + Arrays.toString(RESULTS.get(name))); 89 } 90 91 System.out.println(); 92 for (String name : RESULTS.keySet()) { 93 long[] r = RESULTS.get(name); 94 BigDecimal percent = BigDecimal.valueOf(r[6], 1); 95 String max = (" " + NF.format(r[0])); 96 max = max.substring(max.length() - 12); 97 String min = (" " + NF.format(r[5])); 98 min = min.substring(min.length() - 12); 99 System.out.println(name + "\t" + max + "\t" + min + "\t-" + percent + "%"); 100 } 101 } process()102 public static void process() { 103 LocalTime time = LocalTime.of(12, 30, 20); 104 System.out.println(time); 105 106 List<LocalDateTime> ldt = setupDateTime(); 107 queryListDateTime(ldt); 108 formatListDateTime(ldt); 109 sortListDateTime(ldt); 110 111 List<ZonedDateTime> zdt = setupZonedDateTime(); 112 queryListZonedDateTime(zdt); 113 formatListZonedDateTime(zdt); 114 sortListZonedDateTime(zdt); 115 116 List<Instant> instants = setupInstant(); 117 queryListInstant(instants); 118 formatListInstant(instants); 119 sortListInstant(instants); 120 121 List<Date> judates = setupDate(); 122 queryListDate(judates); 123 formatListDate(judates); 124 sortListDate(judates); 125 126 List<LocalDate> ld = setupLocalDate(); 127 queryListLocalDate(ld); 128 formatListLocalDate(ld); 129 sortListLocalDate(ld); 130 131 List<LocalTime> lt = setupTime(); 132 queryListTime(lt); 133 formatListTime(lt); 134 sortListTime(lt); 135 136 List<GregorianCalendar> gcals = setupGCal(); 137 queryListGCal(gcals); 138 formatListGCal(gcals); 139 sortListGCal(gcals); 140 141 deriveTime(lt); 142 deriveDateTime(ldt); 143 } 144 145 //----------------------------------------------------------------------- setupDateTime()146 private static List<LocalDateTime> setupDateTime() { 147 Random random = new Random(47658758756875687L); 148 List<LocalDateTime> list = new ArrayList<LocalDateTime>(SIZE); 149 long start = System.nanoTime(); 150 for (int i = 0; i < SIZE; i++) { 151 LocalDateTime t = LocalDateTime.of( 152 random.nextInt(10000), random.nextInt(12) + 1, random.nextInt(28) + 1, 153 random.nextInt(24), random.nextInt(60), random.nextInt(60)); 154 list.add(t); 155 } 156 long end = System.nanoTime(); 157 System.out.println("LocalDT: Setup: " + NF.format(end - start) + " ns"); 158 result("LocalDT-I", end - start); 159 return list; 160 } 161 sortListDateTime(List<LocalDateTime> list)162 private static void sortListDateTime(List<LocalDateTime> list) { 163 long start = System.nanoTime(); 164 Collections.sort(list); 165 long end = System.nanoTime(); 166 System.out.println("LocalDT: Sort: " + NF.format(end - start) + " ns " + list.get(0)); 167 result("LocalDT-S", end - start); 168 } 169 queryListDateTime(List<LocalDateTime> list)170 private static void queryListDateTime(List<LocalDateTime> list) { 171 long total = 0; 172 long start = System.nanoTime(); 173 for (LocalDateTime dt : list) { 174 total += dt.getYear(); 175 total += dt.getMonth().getValue(); 176 total += dt.getDayOfMonth(); 177 total += dt.getHour(); 178 total += dt.getMinute(); 179 total += dt.getSecond(); 180 } 181 long end = System.nanoTime(); 182 System.out.println("LocalDT: Query: " + NF.format(end - start) + " ns" + " " + total); 183 result("LocalDT-Q", end - start); 184 } 185 formatListDateTime(List<LocalDateTime> list)186 private static void formatListDateTime(List<LocalDateTime> list) { 187 StringBuilder buf = new StringBuilder(); 188 DateTimeFormatter format = DateTimeFormatter.ISO_DATE.withLocale(Locale.ENGLISH); 189 long start = System.nanoTime(); 190 for (LocalDateTime dt : list) { 191 buf.setLength(0); 192 buf.append(format.format(dt)); 193 } 194 long end = System.nanoTime(); 195 System.out.println("LocalDT: Format: " + NF.format(end - start) + " ns" + " " + buf); 196 result("LocalDT-P", end - start); 197 } 198 deriveDateTime(List<LocalDateTime> list)199 private static void deriveDateTime(List<LocalDateTime> list) { 200 long total = 0; 201 long start = System.nanoTime(); 202 for (LocalDateTime dt : list) { 203 total += dt.get(YEAR); 204 total += dt.get(MONTH_OF_YEAR); 205 total += dt.get(DAY_OF_MONTH); 206 total += dt.get(HOUR_OF_DAY); 207 total += dt.get(MINUTE_OF_HOUR); 208 } 209 long end = System.nanoTime(); 210 System.out.println("LocalDT: Derive: " + NF.format(end - start) + " ns" + " " + total); 211 result("LocalDT-V", end - start); 212 } 213 214 //----------------------------------------------------------------------- setupLocalDate()215 private static List<LocalDate> setupLocalDate() { 216 Random random = new Random(47658758756875687L); 217 List<LocalDate> list = new ArrayList<LocalDate>(SIZE); 218 long start = System.nanoTime(); 219 for (int i = 0; i < SIZE; i++) { 220 LocalDate t = LocalDate.of(random.nextInt(10000), random.nextInt(12) + 1, random.nextInt(28) + 1); 221 list.add(t); 222 } 223 long end = System.nanoTime(); 224 System.out.println("LocalD: Setup: " + NF.format(end - start) + " ns"); 225 result("LocalD-I", end - start); 226 return list; 227 } 228 sortListLocalDate(List<LocalDate> list)229 private static void sortListLocalDate(List<LocalDate> list) { 230 long start = System.nanoTime(); 231 Collections.sort(list); 232 long end = System.nanoTime(); 233 System.out.println("LocalD: Sort: " + NF.format(end - start) + " ns " + list.get(0)); 234 result("LocalD-S", end - start); 235 } 236 queryListLocalDate(List<LocalDate> list)237 private static void queryListLocalDate(List<LocalDate> list) { 238 long total = 0; 239 long start = System.nanoTime(); 240 for (LocalDate dt : list) { 241 total += dt.getYear(); 242 total += dt.getMonth().getValue(); 243 total += dt.getDayOfMonth(); 244 } 245 long end = System.nanoTime(); 246 System.out.println("LocalD: Query: " + NF.format(end - start) + " ns" + " " + total); 247 result("LocalD-Q", end - start); 248 } 249 formatListLocalDate(List<LocalDate> list)250 private static void formatListLocalDate(List<LocalDate> list) { 251 StringBuilder buf = new StringBuilder(); 252 DateTimeFormatter format = DateTimeFormatter.ISO_DATE.withLocale(Locale.ENGLISH); 253 long start = System.nanoTime(); 254 for (LocalDate dt : list) { 255 buf.setLength(0); 256 buf.append(format.format(dt)); 257 } 258 long end = System.nanoTime(); 259 System.out.println("LocalD: Format: " + NF.format(end - start) + " ns" + " " + buf); 260 result("LocalD-P", end - start); 261 } 262 263 //----------------------------------------------------------------------- setupTime()264 private static List<LocalTime> setupTime() { 265 Random random = new Random(47658758756875687L); 266 List<LocalTime> list = new ArrayList<LocalTime>(SIZE); 267 long start = System.nanoTime(); 268 for (int i = 0; i < SIZE; i++) { 269 LocalTime t = LocalTime.of(random.nextInt(24), random.nextInt(60), random.nextInt(60), random.nextInt(1000000000)); 270 list.add(t); 271 } 272 long end = System.nanoTime(); 273 System.out.println("LocalT: Setup: " + NF.format(end - start) + " ns"); 274 result("LocalT-I", end - start); 275 return list; 276 } 277 sortListTime(List<LocalTime> list)278 private static void sortListTime(List<LocalTime> list) { 279 long start = System.nanoTime(); 280 Collections.sort(list); 281 long end = System.nanoTime(); 282 System.out.println("LocalT: Sort: " + NF.format(end - start) + " ns " + list.get(0)); 283 result("LocalT-S", end - start); 284 } 285 queryListTime(List<LocalTime> list)286 private static void queryListTime(List<LocalTime> list) { 287 long total = 0; 288 long start = System.nanoTime(); 289 for (LocalTime dt : list) { 290 total += dt.getHour(); 291 total += dt.getMinute(); 292 total += dt.getSecond(); 293 total += dt.getNano(); 294 } 295 long end = System.nanoTime(); 296 System.out.println("LocalT: Query: " + NF.format(end - start) + " ns" + " " + total); 297 result("LocalT-Q", end - start); 298 } 299 formatListTime(List<LocalTime> list)300 private static void formatListTime(List<LocalTime> list) { 301 StringBuilder buf = new StringBuilder(); 302 DateTimeFormatter format = DateTimeFormatter.ISO_TIME.withLocale(Locale.ENGLISH); 303 long start = System.nanoTime(); 304 for (LocalTime dt : list) { 305 buf.setLength(0); 306 buf.append(format.format(dt)); 307 } 308 long end = System.nanoTime(); 309 System.out.println("LocalT: Format: " + NF.format(end - start) + " ns" + " " + buf); 310 result("LocalT-P", end - start); 311 } 312 deriveTime(List<LocalTime> list)313 private static void deriveTime(List<LocalTime> list) { 314 long total = 0; 315 long start = System.nanoTime(); 316 for (LocalTime dt : list) { 317 total += dt.get(HOUR_OF_DAY); 318 total += dt.get(MINUTE_OF_HOUR); 319 total += dt.get(SECOND_OF_MINUTE); 320 total += dt.get(NANO_OF_SECOND); 321 } 322 long end = System.nanoTime(); 323 System.out.println("LocalT: Derive: " + NF.format(end - start) + " ns" + " " + total); 324 result("LocalT-V", end - start); 325 } 326 327 //----------------------------------------------------------------------- setupZonedDateTime()328 private static List<ZonedDateTime> setupZonedDateTime() { 329 ZoneId tz = ZoneId.of("Europe/London"); 330 Random random = new Random(47658758756875687L); 331 List<ZonedDateTime> list = new ArrayList<ZonedDateTime>(SIZE); 332 long start = System.nanoTime(); 333 for (int i = 0; i < SIZE; i++) { 334 ZonedDateTime t = LocalDateTime.of( 335 2008/*random.nextInt(10000)*/, random.nextInt(12) + 1, random.nextInt(28) + 1, 336 random.nextInt(24), random.nextInt(60), random.nextInt(60), 0).atZone(tz); 337 list.add(t); 338 } 339 long end = System.nanoTime(); 340 System.out.println("ZonedDT: Setup: " + NF.format(end - start) + " ns"); 341 result("ZonedDT-I", end - start); 342 return list; 343 } 344 sortListZonedDateTime(List<ZonedDateTime> list)345 private static void sortListZonedDateTime(List<ZonedDateTime> list) { 346 long start = System.nanoTime(); 347 Collections.sort(list); 348 long end = System.nanoTime(); 349 System.out.println("ZonedDT: Sort: " + NF.format(end - start) + " ns"); 350 result("ZonedDT-S", end - start); 351 } 352 queryListZonedDateTime(List<ZonedDateTime> list)353 private static void queryListZonedDateTime(List<ZonedDateTime> list) { 354 long total = 0; 355 long start = System.nanoTime(); 356 for (ZonedDateTime dt : list) { 357 total += dt.getYear(); 358 total += dt.getMonth().getValue(); 359 total += dt.getDayOfMonth(); 360 total += dt.getHour(); 361 total += dt.getMinute(); 362 total += dt.getSecond(); 363 } 364 long end = System.nanoTime(); 365 System.out.println("ZonedDT: Query: " + NF.format(end - start) + " ns" + " " + total); 366 result("ZonedDT-Q", end - start); 367 } 368 formatListZonedDateTime(List<ZonedDateTime> list)369 private static void formatListZonedDateTime(List<ZonedDateTime> list) { 370 StringBuilder buf = new StringBuilder(); 371 DateTimeFormatter format = DateTimeFormatter.ISO_DATE.withLocale(Locale.ENGLISH); 372 long start = System.nanoTime(); 373 for (ZonedDateTime dt : list) { 374 buf.setLength(0); 375 buf.append(format.format(dt)); 376 } 377 long end = System.nanoTime(); 378 System.out.println("ZonedDT: Format: " + NF.format(end - start) + " ns" + " " + buf); 379 result("ZonedDT-P", end - start); 380 } 381 382 //----------------------------------------------------------------------- setupInstant()383 private static List<Instant> setupInstant() { 384 Random random = new Random(47658758756875687L); 385 List<Instant> list = new ArrayList<Instant>(SIZE); 386 long start = System.nanoTime(); 387 for (int i = 0; i < SIZE; i++) { 388 Instant t = Instant.ofEpochMilli(random.nextLong()); 389 list.add(t); 390 } 391 long end = System.nanoTime(); 392 System.out.println("Instant: Setup: " + NF.format(end - start) + " ns"); 393 result("Instant-I", end - start); 394 return list; 395 } 396 sortListInstant(List<Instant> list)397 private static void sortListInstant(List<Instant> list) { 398 long start = System.nanoTime(); 399 Collections.sort(list); 400 long end = System.nanoTime(); 401 System.out.println("Instant: Sort: " + NF.format(end - start) + " ns"); 402 result("Instant-S", end - start); 403 } 404 queryListInstant(List<Instant> list)405 private static void queryListInstant(List<Instant> list) { 406 long total = 0; 407 long start = System.nanoTime(); 408 for (Instant dt : list) { 409 total += dt.getEpochSecond(); 410 total += dt.getNano(); 411 } 412 long end = System.nanoTime(); 413 System.out.println("Instant: Query: " + NF.format(end - start) + " ns" + " " + total); 414 result("Instant-Q", end - start); 415 } 416 formatListInstant(List<Instant> list)417 private static void formatListInstant(List<Instant> list) { 418 StringBuilder buf = new StringBuilder(); 419 long start = System.nanoTime(); 420 for (Instant dt : list) { 421 buf.setLength(0); 422 buf.append(dt.toString()); 423 } 424 long end = System.nanoTime(); 425 System.out.println("Instant: Format: " + NF.format(end - start) + " ns" + " " + buf); 426 result("Instant-P", end - start); 427 } 428 429 //----------------------------------------------------------------------- setupDate()430 private static List<Date> setupDate() { 431 Random random = new Random(47658758756875687L); 432 List<Date> list = new ArrayList<Date>(SIZE); 433 long start = System.nanoTime(); 434 for (int i = 0; i < SIZE; i++) { 435 Date t = new Date(random.nextLong()); 436 list.add(t); 437 } 438 long end = System.nanoTime(); 439 System.out.println("Date: Setup: " + NF.format(end - start) + " ns"); 440 result("JUDate-I", end - start); 441 return list; 442 } 443 sortListDate(List<Date> list)444 private static void sortListDate(List<Date> list) { 445 long start = System.nanoTime(); 446 Collections.sort(list); 447 long end = System.nanoTime(); 448 System.out.println("Date: Sort: " + NF.format(end - start) + " ns " + list.get(0)); 449 result("JUDate-S", end - start); 450 } 451 queryListDate(List<Date> list)452 private static void queryListDate(List<Date> list) { 453 long total = 0; 454 long start = System.nanoTime(); 455 for (Date dt : list) { 456 total += dt.getTime(); 457 } 458 long end = System.nanoTime(); 459 System.out.println("Date: Query: " + NF.format(end - start) + " ns" + " " + total); 460 result("JUDate-Q", end - start); 461 } 462 formatListDate(List<Date> list)463 private static void formatListDate(List<Date> list) { 464 StringBuilder buf = new StringBuilder(); 465 long start = System.nanoTime(); 466 for (Date dt : list) { 467 buf.setLength(0); 468 buf.append(dt.toString()); 469 } 470 long end = System.nanoTime(); 471 System.out.println("Date: Format: " + NF.format(end - start) + " ns" + " " + buf); 472 result("JUDate-P", end - start); 473 } 474 475 //----------------------------------------------------------------------- setupGCal()476 private static List<GregorianCalendar> setupGCal() { 477 java.util.TimeZone tz = java.util.TimeZone.getTimeZone("Europe/London"); 478 Random random = new Random(47658758756875687L); 479 List<GregorianCalendar> list = new ArrayList<GregorianCalendar>(SIZE); 480 long start = System.nanoTime(); 481 for (int i = 0; i < SIZE; i++) { 482 GregorianCalendar t = new GregorianCalendar(tz); 483 t.setGregorianChange(new Date(Long.MIN_VALUE)); 484 t.set(random.nextInt(10000), random.nextInt(12), random.nextInt(28) + 1, random.nextInt(24), random.nextInt(60), random.nextInt(60)); 485 list.add(t); 486 } 487 long end = System.nanoTime(); 488 System.out.println("GCalendar: Setup: " + NF.format(end - start) + " ns"); 489 result("GregCal-I", end - start); 490 return list; 491 } 492 sortListGCal(List<GregorianCalendar> list)493 private static void sortListGCal(List<GregorianCalendar> list) { 494 long start = System.nanoTime(); 495 Collections.sort(list); 496 long end = System.nanoTime(); 497 System.out.println("GCalendar: Sort: " + NF.format(end - start) + " ns"); 498 result("GregCal-S", end - start); 499 } 500 queryListGCal(List<GregorianCalendar> list)501 private static void queryListGCal(List<GregorianCalendar> list) { 502 long total = 0; 503 long start = System.nanoTime(); 504 for (GregorianCalendar gcal : list) { 505 total += gcal.get(Calendar.YEAR); 506 total += gcal.get(Calendar.MONTH + 1); 507 total += gcal.get(Calendar.DAY_OF_MONTH); 508 total += gcal.get(Calendar.HOUR_OF_DAY); 509 total += gcal.get(Calendar.MINUTE); 510 total += gcal.get(Calendar.SECOND); 511 total += gcal.get(Calendar.SECOND); 512 } 513 long end = System.nanoTime(); 514 System.out.println("GCalendar: Query: " + NF.format(end - start) + " ns" + " " + total); 515 result("GregCal-Q", end - start); 516 } 517 formatListGCal(List<GregorianCalendar> list)518 private static void formatListGCal(List<GregorianCalendar> list) { 519 StringBuilder buf = new StringBuilder(); 520 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); 521 long start = System.nanoTime(); 522 for (GregorianCalendar gcal : list) { 523 buf.setLength(0); 524 buf.append(format.format(gcal.getTime())); 525 } 526 long end = System.nanoTime(); 527 System.out.println("GCalendar: Format: " + NF.format(end - start) + " ns" + " " + buf); 528 result("GregCal-P", end - start); 529 } 530 531 //----------------------------------------------------------------------- result(String name, long result)532 private static void result(String name, long result) { 533 long[] values = RESULTS.get(name); 534 if (values == null) { 535 values = new long[7]; 536 RESULTS.put(name, values); 537 } 538 values[loop] = result; 539 if (loop == 4) { 540 values[5] = Math.min(values[0], Math.min(values[1], Math.min(values[2], Math.min(values[3], values[4])))); 541 values[6] = (((values[0] - values[5]) * 1000) / values[0]); 542 } 543 } 544 545 } 546