001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.lang;
019:
020: import java.text.NumberFormat;
021: import java.util.Calendar;
022:
023: /**
024: * Tests the difference in performance between CharUtils and CharSet.
025: *
026: * Sample runs:
027:
028: Now: Thu Mar 18 14:29:48 PST 2004
029: Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.3.1_10-b03
030: Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.3.1_10-b03
031: Windows XP 5.1 x86 pentium i486 i386
032: Do nohting: 0 milliseconds.
033: run_CharUtils_isAsciiNumeric: 4,545 milliseconds.
034: run_inlined_CharUtils_isAsciiNumeric: 3,417 milliseconds.
035: run_inlined_CharUtils_isAsciiNumeric: 85,679 milliseconds.
036:
037:
038: Now: Thu Mar 18 14:24:51 PST 2004
039: Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.4.2_04-b05
040: Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.4.2_04-b05
041: Windows XP 5.1 x86 pentium i486 i386
042: Do nohting: 0 milliseconds.
043: run_CharUtils_isAsciiNumeric: 2,578 milliseconds.
044: run_inlined_CharUtils_isAsciiNumeric: 2,477 milliseconds.
045: run_inlined_CharUtils_isAsciiNumeric: 114,429 milliseconds.
046:
047: Now: Thu Mar 18 14:27:55 PST 2004
048: Sun Microsystems Inc. Java(TM) 2 Runtime Environment, Standard Edition 1.4.2_04-b05
049: Sun Microsystems Inc. Java HotSpot(TM) Server VM 1.4.2_04-b05
050: Windows XP 5.1 x86 pentium i486 i386
051: Do nohting: 0 milliseconds.
052: run_CharUtils_isAsciiNumeric: 630 milliseconds.
053: run_inlined_CharUtils_isAsciiNumeric: 709 milliseconds.
054: run_inlined_CharUtils_isAsciiNumeric: 84,420 milliseconds.
055:
056:
057: * @version $Id: CharUtilsPerfTest.java 437554 2006-08-28 06:21:41Z bayard $
058: */
059: public class CharUtilsPerfTest {
060: final static String VERSION = "$Id: CharUtilsPerfTest.java 437554 2006-08-28 06:21:41Z bayard $";
061:
062: final static int WARM_UP = 100;
063:
064: final static int COUNT = 5000;
065:
066: final static char[] CHAR_SAMPLES;
067: static {
068: CHAR_SAMPLES = new char[Character.MAX_VALUE];
069: for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
070: CHAR_SAMPLES[i] = i;
071: }
072: }
073:
074: public static void main(String[] args) {
075: new CharUtilsPerfTest().run();
076: }
077:
078: private void printSysInfo() {
079: System.out.println(VERSION);
080: System.out.println("Now: " + Calendar.getInstance().getTime());
081: System.out.println(System.getProperty("java.vendor") + " "
082: + System.getProperty("java.runtime.name") + " "
083: + System.getProperty("java.runtime.version"));
084: System.out.println(System.getProperty("java.vm.vendor") + " "
085: + System.getProperty("java.vm.name") + " "
086: + System.getProperty("java.vm.version"));
087: System.out.println(System.getProperty("os.name") + " "
088: + System.getProperty("os.version") + " "
089: + System.getProperty("os.arch") + " "
090: + System.getProperty("sun.cpu.isalist"));
091: }
092:
093: private void run() {
094: this .printSysInfo();
095: long start;
096: start = System.currentTimeMillis();
097: this .printlnTotal("Do nohting", start);
098: //System.out.println("Warming up...");
099: run_CharUtils_isAsciiNumeric(WARM_UP);
100: //System.out.println("Measuring...");
101: start = System.currentTimeMillis();
102: run_CharUtils_isAsciiNumeric(COUNT);
103: this .printlnTotal("run_CharUtils_isAsciiNumeric", start);
104: //System.out.println("Warming up...");
105: run_inlined_CharUtils_isAsciiNumeric(WARM_UP);
106: //System.out.println("Measuring...");
107: start = System.currentTimeMillis();
108: run_inlined_CharUtils_isAsciiNumeric(COUNT);
109: this
110: .printlnTotal("run_inlined_CharUtils_isAsciiNumeric",
111: start);
112: //System.out.println("Warming up...");
113: run_CharSet(WARM_UP);
114: //System.out.println("Measuring...");
115: start = System.currentTimeMillis();
116: run_CharSet(COUNT);
117: this .printlnTotal("run_CharSet", start);
118: }
119:
120: private int run_CharSet(int loopCount) {
121: int t = 0;
122: for (int i = 0; i < loopCount; i++) {
123: for (int j = 0; j < CHAR_SAMPLES.length; j++) {
124: char ch = CHAR_SAMPLES[j];
125: boolean b = CharSet.ASCII_NUMERIC.contains(ch);
126: t += b ? 1 : 0;
127: }
128: }
129: return t;
130: }
131:
132: private int run_CharUtils_isAsciiNumeric(int loopCount) {
133: int t = 0;
134: for (int i = 0; i < loopCount; i++) {
135: for (int j = 0; j < CHAR_SAMPLES.length; j++) {
136: char ch = CHAR_SAMPLES[j];
137: boolean b = CharUtils.isAsciiNumeric(ch);
138: t += b ? 1 : 0;
139: }
140: }
141: return t;
142: }
143:
144: private int run_inlined_CharUtils_isAsciiNumeric(int loopCount) {
145: int t = 0;
146: for (int i = 0; i < loopCount; i++) {
147: for (int j = 0; j < CHAR_SAMPLES.length; j++) {
148: char ch = CHAR_SAMPLES[j];
149: boolean b = (ch >= '0' && ch <= '9');
150: t += b ? 1 : 0;
151: }
152: }
153: return t;
154: }
155:
156: private void printlnTotal(String prefix, long start) {
157: long total = System.currentTimeMillis() - start;
158: System.out.println(prefix + ": "
159: + NumberFormat.getInstance().format(total)
160: + " milliseconds.");
161: }
162: }
|