1 16 17 package org.apache.commons.lang; 18 19 import java.text.NumberFormat ; 20 import java.util.Calendar ; 21 22 58 public class CharUtilsPerfTest { 59 final static String 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 [] 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 run_CharUtils_isAsciiNumeric(WARM_UP); 106 start = System.currentTimeMillis(); 108 run_CharUtils_isAsciiNumeric(COUNT); 109 this.printlnTotal("run_CharUtils_isAsciiNumeric", start); 110 run_inlined_CharUtils_isAsciiNumeric(WARM_UP); 112 start = System.currentTimeMillis(); 114 run_inlined_CharUtils_isAsciiNumeric(COUNT); 115 this.printlnTotal("run_inlined_CharUtils_isAsciiNumeric", start); 116 run_CharSet(WARM_UP); 118 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 prefix, long start) { 161 long total = System.currentTimeMillis() - start; 162 System.out.println(prefix + ": " + NumberFormat.getInstance().format(total) + " milliseconds."); 163 } 164 } 165 | Popular Tags |