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.lang;
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 nohting: 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 nohting: 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 nohting: 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   * @version $Id: CharUtilsPerfTest.java 437554 2006-08-28 06:21:41Z bayard $
58   */
59  public class CharUtilsPerfTest {
60      final static String VERSION = "$Id: CharUtilsPerfTest.java 437554 2006-08-28 06:21:41Z bayard $";
61  
62      final static int WARM_UP = 100;
63  
64      final static int COUNT = 5000;
65  
66      final static char[] CHAR_SAMPLES;
67      static {
68          CHAR_SAMPLES = new char[Character.MAX_VALUE];
69          for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
70              CHAR_SAMPLES[i] = i;
71          }
72      }
73  
74      public static void main(String[] args) {
75          new CharUtilsPerfTest().run();
76      }
77  
78      private void printSysInfo() {
79          System.out.println(VERSION);
80          System.out.println("Now: " + Calendar.getInstance().getTime());
81          System.out.println(System.getProperty("java.vendor")
82                  + " "
83                  + System.getProperty("java.runtime.name")
84                  + " "
85                  + System.getProperty("java.runtime.version"));
86          System.out.println(System.getProperty("java.vm.vendor")
87                  + " "
88                  + System.getProperty("java.vm.name")
89                  + " "
90                  + System.getProperty("java.vm.version"));
91          System.out.println(System.getProperty("os.name")
92              + " "
93              + System.getProperty("os.version")
94              + " "
95              + System.getProperty("os.arch")
96              + " "
97              + System.getProperty("sun.cpu.isalist"));
98      }
99  
100     private void run() {
101         this.printSysInfo();
102         long start;
103         start = System.currentTimeMillis();
104         this.printlnTotal("Do nohting", start);
105         //System.out.println("Warming up...");
106         run_CharUtils_isAsciiNumeric(WARM_UP);
107         //System.out.println("Measuring...");
108         start = System.currentTimeMillis();
109         run_CharUtils_isAsciiNumeric(COUNT);
110         this.printlnTotal("run_CharUtils_isAsciiNumeric", start);
111         //System.out.println("Warming up...");
112         run_inlined_CharUtils_isAsciiNumeric(WARM_UP);
113         //System.out.println("Measuring...");
114         start = System.currentTimeMillis();
115         run_inlined_CharUtils_isAsciiNumeric(COUNT);
116         this.printlnTotal("run_inlined_CharUtils_isAsciiNumeric", start);
117         //System.out.println("Warming up...");
118         run_CharSet(WARM_UP);
119         //System.out.println("Measuring...");
120         start = System.currentTimeMillis();
121         run_CharSet(COUNT);
122         this.printlnTotal("run_CharSet", start);
123     }
124 
125     private int run_CharSet(int loopCount) {
126         int t = 0;
127         for (int i = 0; i < loopCount; i++) {
128             for (int j = 0; j < CHAR_SAMPLES.length; j++) {
129                 char ch = CHAR_SAMPLES[j];
130                 boolean b = CharSet.ASCII_NUMERIC.contains(ch);
131                 t += b ? 1 : 0;
132             }
133         }
134         return t;
135     }
136 
137     private int run_CharUtils_isAsciiNumeric(int loopCount) {
138         int t = 0;
139         for (int i = 0; i < loopCount; i++) {
140             for (int j = 0; j < CHAR_SAMPLES.length; j++) {
141                 char ch = CHAR_SAMPLES[j];
142                 boolean b = CharUtils.isAsciiNumeric(ch);
143                 t += b ? 1 : 0;
144             }
145         }
146         return t;
147     }
148 
149     private int run_inlined_CharUtils_isAsciiNumeric(int loopCount) {
150         int t = 0;
151         for (int i = 0; i < loopCount; i++) {
152             for (int j = 0; j < CHAR_SAMPLES.length; j++) {
153                 char ch = CHAR_SAMPLES[j];
154                 boolean b = (ch >= '0' && ch <= '9');
155                 t += b ? 1 : 0;
156             }
157         }
158         return t;
159     }
160 
161     private void printlnTotal(String prefix, long start) {
162         long total = System.currentTimeMillis() - start;
163         System.out.println(prefix + ": " + NumberFormat.getInstance().format(total) + " milliseconds.");
164     }
165 }