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  
38  Now: Thu Mar 18 14:24:51 PST 2004
39  Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.4.2_04-b05
40  Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.4.2_04-b05
41  Windows XP 5.1 x86 pentium i486 i386
42  Do nothing: 0 milliseconds.
43  run_CharUtils_isAsciiNumeric: 2,578 milliseconds.
44  run_inlined_CharUtils_isAsciiNumeric: 2,477 milliseconds.
45  run_inlined_CharUtils_isAsciiNumeric: 114,429 milliseconds.
46  
47  Now: Thu Mar 18 14:27:55 PST 2004
48  Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.4.2_04-b05
49  Sun Microsystems Inc. Java HotSpot(TM) Server VM 1.4.2_04-b05
50  Windows XP 5.1 x86 pentium i486 i386
51  Do nothing: 0 milliseconds.
52  run_CharUtils_isAsciiNumeric: 630 milliseconds.
53  run_inlined_CharUtils_isAsciiNumeric: 709 milliseconds.
54  run_inlined_CharUtils_isAsciiNumeric: 84,420 milliseconds.
55  
56  
57   */
58  public class CharUtilsPerfRun {
59  
60      private static final int WARM_UP = 100;
61  
62      private static final int COUNT = 5000;
63  
64      private static final char[] ALL_CHARS;
65  
66      static {
67          ALL_CHARS = new char[Character.MAX_VALUE];
68          for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
69              ALL_CHARS[i] = i;
70          }
71      }
72  
73      public static void main(final String[] args) {
74          new CharUtilsPerfRun().run();
75      }
76  
77      private void printlnTotal(final String prefix, final long startMillis) {
78          final long totalMillis = System.currentTimeMillis() - startMillis;
79          System.out.println(prefix + ": " + NumberFormat.getInstance().format(totalMillis) + " milliseconds.");
80      }
81  
82      private void printSysInfo() {
83          System.out.println("Now: " + Calendar.getInstance().getTime());
84          System.out.println(SystemProperties.getJavaVendor()
85                  + " "
86                  + SystemProperties.getJavaRuntimeName()
87                  + " "
88                  + SystemProperties.getJavaRuntimeVersion());
89          System.out.println(SystemProperties.getJavaVmVendor()
90                  + " "
91                  + SystemProperties.getJavaVmName()
92                  + " "
93                  + SystemProperties.getJavaVmVersion());
94          System.out.println(SystemProperties.getOsName()
95              + " "
96              + SystemProperties.getOsVersion()
97              + " "
98              + SystemProperties.getOsArch()
99              + " "
100             + System.getProperty("sun.cpu.isalist"));
101     }
102 
103     private void run() {
104         this.printSysInfo();
105         long startMillis;
106         startMillis = System.currentTimeMillis();
107         this.printlnTotal("Do nothing", startMillis);
108         run_CharUtils_isAsciiNumeric(WARM_UP);
109         startMillis = System.currentTimeMillis();
110         run_CharUtils_isAsciiNumeric(COUNT);
111         this.printlnTotal("run_CharUtils_isAsciiNumeric", startMillis);
112         run_inlined_CharUtils_isAsciiNumeric(WARM_UP);
113         startMillis = System.currentTimeMillis();
114         run_inlined_CharUtils_isAsciiNumeric(COUNT);
115         this.printlnTotal("run_inlined_CharUtils_isAsciiNumeric", startMillis);
116         run_CharSet(WARM_UP);
117         startMillis = System.currentTimeMillis();
118         run_CharSet(COUNT);
119         this.printlnTotal("run_CharSet", startMillis);
120     }
121 
122     private int run_CharSet(final int loopCount) {
123         int t = 0;
124         for (int i = 0; i < loopCount; i++) {
125             for (final char ch : ALL_CHARS) {
126                 final boolean b = CharSet.ASCII_NUMERIC.contains(ch);
127                 t += b ? 1 : 0;
128             }
129         }
130         return t;
131     }
132 
133     private int run_CharUtils_isAsciiNumeric(final int loopCount) {
134         int t = 0;
135         for (int i = 0; i < loopCount; i++) {
136             for (final char ch : ALL_CHARS) {
137                 final boolean b = CharUtils.isAsciiNumeric(ch);
138                 t += b ? 1 : 0;
139             }
140         }
141         return t;
142     }
143 
144     private int run_inlined_CharUtils_isAsciiNumeric(final int loopCount) {
145         int t = 0;
146         for (int i = 0; i < loopCount; i++) {
147             for (final char ch : ALL_CHARS) {
148                 final boolean b = ch >= '0' && ch <= '9';
149                 t += b ? 1 : 0;
150             }
151         }
152         return t;
153     }
154 }