2 * ******************************************************************************
3 * Copyright (C) 2007, International Business Machines Corporation and others. *
4 * All Rights Reserved. *
5 * ******************************************************************************
7 package com.ibm.icu.dev.test.perf;
9 import java.text.ParseException;
10 import java.util.Date;
11 import java.util.Locale;
16 public class DateFormatPerformanceTest extends PerfTest {
17 private String pattern;
19 private String dateString;
23 private com.ibm.icu.text.SimpleDateFormat[] icuDateFormat;
25 private java.text.SimpleDateFormat[] jdkDateFormat;
27 public static void main(String[] args) throws Exception {
28 new DateFormatPerformanceTest().run(args);
31 protected void setup(String[] args) {
33 if (args.length == 0 || args.length > 2) {
34 throw new UsageException();
40 locale = Locale.getDefault();
42 icuDateFormat = new com.ibm.icu.text.SimpleDateFormat[threads];
43 jdkDateFormat = new java.text.SimpleDateFormat[threads];
44 for (int i = 0; i < threads; i++) {
45 icuDateFormat[i] = new com.ibm.icu.text.SimpleDateFormat(pattern, locale);
46 jdkDateFormat[i] = new java.text.SimpleDateFormat(pattern, locale);
49 if (args.length == 2) {
51 date = icuDateFormat[0].parse(dateString);
53 } catch (Exception e) {
55 throw new RuntimeException(e.getMessage());
60 PerfTest.Function TestICUConstruction() {
61 return new PerfTest.Function() {
63 new com.ibm.icu.text.SimpleDateFormat(pattern, locale);
68 PerfTest.Function TestJDKConstruction() {
69 return new PerfTest.Function() {
71 new java.text.SimpleDateFormat(pattern, locale);
76 PerfTest.Function TestICUParse() {
77 return new PerfTest.Function() {
78 public void call(int id) {
80 icuDateFormat[id].parse(dateString);
81 } catch (ParseException ex) {
88 PerfTest.Function TestJDKParse() {
89 return new PerfTest.Function() {
90 public void call(int id) {
92 jdkDateFormat[id].parse(dateString);
93 } catch (ParseException ex) {
100 PerfTest.Function TestICUFormat() {
101 return new PerfTest.Function() {
102 public void call(int id) {
103 icuDateFormat[id].format(date);
108 PerfTest.Function TestJDKFormat() {
109 return new PerfTest.Function() {
110 public void call(int id) {
111 jdkDateFormat[id].format(date);