Home | History | Annotate | Download | only in perf
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  * ******************************************************************************
      5  * Copyright (C) 2007, International Business Machines Corporation and * others.
      6  * All Rights Reserved. *
      7  * ******************************************************************************
      8  */
      9 package com.ibm.icu.dev.test.perf;
     10 
     11 import java.text.ParseException;
     12 import java.util.Locale;
     13 
     14 /**
     15  * @author ajmacher
     16  */
     17 public class DecimalFormatPerformanceTest extends PerfTest {
     18     String pattern;
     19 
     20     String decimalAsString;
     21 
     22     Number decimalAsNumber;
     23 
     24     com.ibm.icu.text.DecimalFormat[] icuDecimalFormat;
     25 
     26     java.text.DecimalFormat[] javaDecimalFormat;
     27 
     28     public static void main(String[] args) throws Exception {
     29         new DecimalFormatPerformanceTest().run(args);
     30     }
     31 
     32     protected void setup(String[] args) {
     33         try {
     34             if (args.length == 0 || args.length > 2) {
     35                 throw new UsageException();
     36             }
     37 
     38             pattern = args[0];
     39 
     40             if (locale == null)
     41                 locale = Locale.getDefault();
     42 
     43             icuDecimalFormat = new com.ibm.icu.text.DecimalFormat[threads];
     44             javaDecimalFormat = new java.text.DecimalFormat[threads];
     45             for (int i = 0; i < threads; i++) {
     46                 icuDecimalFormat[i] = new com.ibm.icu.text.DecimalFormat(pattern,
     47                         new com.ibm.icu.text.DecimalFormatSymbols(locale));
     48                 javaDecimalFormat[i] = new java.text.DecimalFormat(pattern,
     49                         new java.text.DecimalFormatSymbols(locale));
     50             }
     51 
     52             if (args.length == 2) {
     53                 decimalAsString = args[1];
     54                 decimalAsNumber = icuDecimalFormat[0].parse(decimalAsString);
     55             }
     56         } catch (Exception e) {
     57             e.printStackTrace();
     58             throw new RuntimeException(e.getMessage());
     59         }
     60 
     61     }
     62 
     63     PerfTest.Function TestICUConstruction() {
     64         return new PerfTest.Function() {
     65             public void call() {
     66                 new com.ibm.icu.text.DecimalFormat(pattern,
     67                         new com.ibm.icu.text.DecimalFormatSymbols(locale));
     68             }
     69         };
     70     }
     71 
     72     PerfTest.Function TestJDKConstruction() {
     73         return new PerfTest.Function() {
     74             public void call() {
     75                 new java.text.DecimalFormat(pattern, new java.text.DecimalFormatSymbols(locale));
     76             }
     77         };
     78     }
     79 
     80     PerfTest.Function TestICUParse() {
     81         return new PerfTest.Function() {
     82             public void call(int id) {
     83                 try {
     84                     icuDecimalFormat[id].parse(decimalAsString);
     85                 } catch (ParseException e) {
     86                     e.printStackTrace();
     87                     throw new RuntimeException(e.getMessage());
     88                 }
     89             }
     90         };
     91     }
     92 
     93     PerfTest.Function TestJDKParse() {
     94         return new PerfTest.Function() {
     95             public void call(int id) {
     96                 try {
     97                     javaDecimalFormat[id].parse(decimalAsString);
     98                 } catch (ParseException e) {
     99                     e.printStackTrace();
    100                     throw new RuntimeException(e.getMessage());
    101                 }
    102             }
    103         };
    104     }
    105 
    106     PerfTest.Function TestICUFormat() {
    107         return new PerfTest.Function() {
    108             public void call(int id) {
    109                 icuDecimalFormat[id].format(decimalAsNumber);
    110             }
    111         };
    112     }
    113 
    114     PerfTest.Function TestJDKFormat() {
    115         return new PerfTest.Function() {
    116             public void call(int id) {
    117                 javaDecimalFormat[id].format(decimalAsNumber);
    118             }
    119         };
    120     }
    121 }
    122