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 private static final String VERSION = "$Id$"; 60 61 private static final int WARM_UP = 100; 62 63 private static final int COUNT = 5000; 64 65 private static final char[] CHAR_SAMPLES; 66 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 main(final String[] args)74 public static void main(final String[] args) { 75 new CharUtilsPerfRun().run(); 76 } 77 printSysInfo()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 run()100 private void run() { 101 this.printSysInfo(); 102 long startMillis; 103 startMillis = System.currentTimeMillis(); 104 this.printlnTotal("Do nothing", startMillis); 105 run_CharUtils_isAsciiNumeric(WARM_UP); 106 startMillis = System.currentTimeMillis(); 107 run_CharUtils_isAsciiNumeric(COUNT); 108 this.printlnTotal("run_CharUtils_isAsciiNumeric", startMillis); 109 run_inlined_CharUtils_isAsciiNumeric(WARM_UP); 110 startMillis = System.currentTimeMillis(); 111 run_inlined_CharUtils_isAsciiNumeric(COUNT); 112 this.printlnTotal("run_inlined_CharUtils_isAsciiNumeric", startMillis); 113 run_CharSet(WARM_UP); 114 startMillis = System.currentTimeMillis(); 115 run_CharSet(COUNT); 116 this.printlnTotal("run_CharSet", startMillis); 117 } 118 run_CharSet(final int loopCount)119 private int run_CharSet(final int loopCount) { 120 int t = 0; 121 for (int i = 0; i < loopCount; i++) { 122 for (final char ch : CHAR_SAMPLES) { 123 final boolean b = CharSet.ASCII_NUMERIC.contains(ch); 124 t += b ? 1 : 0; 125 } 126 } 127 return t; 128 } 129 run_CharUtils_isAsciiNumeric(final int loopCount)130 private int run_CharUtils_isAsciiNumeric(final int loopCount) { 131 int t = 0; 132 for (int i = 0; i < loopCount; i++) { 133 for (final char ch : CHAR_SAMPLES) { 134 final boolean b = CharUtils.isAsciiNumeric(ch); 135 t += b ? 1 : 0; 136 } 137 } 138 return t; 139 } 140 run_inlined_CharUtils_isAsciiNumeric(final int loopCount)141 private int run_inlined_CharUtils_isAsciiNumeric(final int loopCount) { 142 int t = 0; 143 for (int i = 0; i < loopCount; i++) { 144 for (final char ch : CHAR_SAMPLES) { 145 final boolean b = ch >= '0' && ch <= '9'; 146 t += b ? 1 : 0; 147 } 148 } 149 return t; 150 } 151 printlnTotal(final String prefix, final long startMillis)152 private void printlnTotal(final String prefix, final long startMillis) { 153 final long totalMillis = System.currentTimeMillis() - startMillis; 154 System.out.println(prefix + ": " + NumberFormat.getInstance().format(totalMillis) + " milliseconds."); 155 } 156 } 157