View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import java.text.NumberFormat;
21  import java.util.Calendar;
22  
23  /**
24   * Tests the difference in performance between CharUtils and CharSet.
25   *
26   * Sample runs:
27  
28  Now: Thu Mar 18 14:29:48 PST 2004
29  Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.3.1_10-b03
30  Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.3.1_10-b03
31  Windows XP 5.1 x86 pentium i486 i386
32  Do nothing: 0 milliseconds.
33  run_CharUtils_isAsciiNumeric: 4,545 milliseconds.
34  run_inlined_CharUtils_isAsciiNumeric: 3,417 milliseconds.
35  run_inlined_CharUtils_isAsciiNumeric: 85,679 milliseconds.
36  
37  Now: Thu Mar 18 14:24:51 PST 2004
38  Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.4.2_04-b05
39  Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.4.2_04-b05
40  Windows XP 5.1 x86 pentium i486 i386
41  Do nothing: 0 milliseconds.
42  run_CharUtils_isAsciiNumeric: 2,578 milliseconds.
43  run_inlined_CharUtils_isAsciiNumeric: 2,477 milliseconds.
44  run_inlined_CharUtils_isAsciiNumeric: 114,429 milliseconds.
45  
46  Now: Thu Mar 18 14:27:55 PST 2004
47  Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.4.2_04-b05
48  Sun Microsystems Inc. Java HotSpot(TM) Server VM 1.4.2_04-b05
49  Windows XP 5.1 x86 pentium i486 i386
50  Do nothing: 0 milliseconds.
51  run_CharUtils_isAsciiNumeric: 630 milliseconds.
52  run_inlined_CharUtils_isAsciiNumeric: 709 milliseconds.
53  run_inlined_CharUtils_isAsciiNumeric: 84,420 milliseconds.
54  
55   */
56  public class CharUtilsPerfRun {
57  
58      private static final int WARM_UP = 100;
59  
60      private static final int COUNT = 5000;
61  
62      private static final char[] ALL_CHARS;
63  
64      static {
65          ALL_CHARS = new char[Character.MAX_VALUE];
66          for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
67              ALL_CHARS[i] = i;
68          }
69      }
70  
71      public static void main(final String[] args) {
72          new CharUtilsPerfRun().run();
73      }
74  
75      private void printlnTotal(final String prefix, final long startMillis) {
76          final long totalMillis = System.currentTimeMillis() - startMillis;
77          System.out.println(prefix + ": " + NumberFormat.getInstance().format(totalMillis) + " milliseconds.");
78      }
79  
80      private void printSysInfo() {
81          System.out.println("Now: " + Calendar.getInstance().getTime());
82          System.out.println(SystemProperties.getJavaVendor()
83                  + " "
84                  + SystemProperties.getJavaRuntimeName()
85                  + " "
86                  + SystemProperties.getJavaRuntimeVersion());
87          System.out.println(SystemProperties.getJavaVmVendor()
88                  + " "
89                  + SystemProperties.getJavaVmName()
90                  + " "
91                  + SystemProperties.getJavaVmVersion());
92          System.out.println(SystemProperties.getOsName()
93              + " "
94              + SystemProperties.getOsVersion()
95              + " "
96              + SystemProperties.getOsArch()
97              + " "
98              + System.getProperty("sun.cpu.isalist"));
99      }
100 
101     private void run() {
102         this.printSysInfo();
103         long startMillis;
104         startMillis = System.currentTimeMillis();
105         this.printlnTotal("Do nothing", startMillis);
106         run_CharUtils_isAsciiNumeric(WARM_UP);
107         startMillis = System.currentTimeMillis();
108         run_CharUtils_isAsciiNumeric(COUNT);
109         this.printlnTotal("run_CharUtils_isAsciiNumeric", startMillis);
110         run_inlined_CharUtils_isAsciiNumeric(WARM_UP);
111         startMillis = System.currentTimeMillis();
112         run_inlined_CharUtils_isAsciiNumeric(COUNT);
113         this.printlnTotal("run_inlined_CharUtils_isAsciiNumeric", startMillis);
114         run_CharSet(WARM_UP);
115         startMillis = System.currentTimeMillis();
116         run_CharSet(COUNT);
117         this.printlnTotal("run_CharSet", startMillis);
118     }
119 
120     private int run_CharSet(final int loopCount) {
121         int t = 0;
122         for (int i = 0; i < loopCount; i++) {
123             for (final char ch : ALL_CHARS) {
124                 final boolean b = CharSet.ASCII_NUMERIC.contains(ch);
125                 t += b ? 1 : 0;
126             }
127         }
128         return t;
129     }
130 
131     private int run_CharUtils_isAsciiNumeric(final int loopCount) {
132         int t = 0;
133         for (int i = 0; i < loopCount; i++) {
134             for (final char ch : ALL_CHARS) {
135                 final boolean b = CharUtils.isAsciiNumeric(ch);
136                 t += b ? 1 : 0;
137             }
138         }
139         return t;
140     }
141 
142     private int run_inlined_CharUtils_isAsciiNumeric(final int loopCount) {
143         int t = 0;
144         for (int i = 0; i < loopCount; i++) {
145             for (final char ch : ALL_CHARS) {
146                 final boolean b = ch >= '0' && ch <= '9';
147                 t += b ? 1 : 0;
148             }
149         }
150         return t;
151     }
152 }