KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > CharUtilsPerfTest


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.lang;
18
19 import java.text.NumberFormat JavaDoc;
20 import java.util.Calendar JavaDoc;
21
22 /**
23  * Tests the difference in performance between CharUtils and CharSet.
24  *
25  * Sample runs:
26
27 Now: Thu Mar 18 14:29:48 PST 2004
28 Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.3.1_10-b03
29 Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.3.1_10-b03
30 Windows XP 5.1 x86 pentium i486 i386
31 Do nohting: 0 milliseconds.
32 run_CharUtils_isAsciiNumeric: 4,545 milliseconds.
33 run_inlined_CharUtils_isAsciiNumeric: 3,417 milliseconds.
34 run_inlined_CharUtils_isAsciiNumeric: 85,679 milliseconds.
35
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 nohting: 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 nohting: 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  * @version $Id: CharUtilsPerfTest.java 161244 2005-04-14 06:16:36Z ggregory $
57  */

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