1 /* 2 * Copyright (C) 2009 Google Inc. 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 benchmarks.regression; 18 19 import com.google.caliper.BeforeExperiment; 20 import com.google.caliper.Param; 21 22 /** 23 * Tests various Character methods, intended for testing multiple 24 * implementations against each other. 25 */ 26 public class CharacterBenchmark { 27 28 @Param private CharacterSet characterSet; 29 30 @Param private Overload overload; 31 32 private char[] chars; 33 34 @BeforeExperiment setUp()35 protected void setUp() throws Exception { 36 this.chars = characterSet.chars; 37 } 38 39 public enum Overload { CHAR, INT } 40 nanosToUnits(double nanos)41 public double nanosToUnits(double nanos) { 42 return nanos / 65536; 43 } 44 45 public enum CharacterSet { 46 ASCII(128), 47 UNICODE(65536); 48 final char[] chars; CharacterSet(int size)49 CharacterSet(int size) { 50 this.chars = new char[65536]; 51 for (int i = 0; i < 65536; ++i) { 52 chars[i] = (char) (i % size); 53 } 54 } 55 } 56 57 // A fake benchmark to give us a baseline. timeIsSpace(int reps)58 public boolean timeIsSpace(int reps) { 59 boolean fake = false; 60 if (overload == Overload.CHAR) { 61 for (int i = 0; i < reps; ++i) { 62 for (int ch = 0; ch < 65536; ++ch) { 63 fake ^= ((char) ch == ' '); 64 } 65 } 66 } else { 67 for (int i = 0; i < reps; ++i) { 68 for (int ch = 0; ch < 65536; ++ch) { 69 fake ^= (ch == ' '); 70 } 71 } 72 } 73 return fake; 74 } 75 timeDigit(int reps)76 public void timeDigit(int reps) { 77 if (overload == Overload.CHAR) { 78 for (int i = 0; i < reps; ++i) { 79 for (int ch = 0; ch < 65536; ++ch) { 80 Character.digit(chars[ch], 10); 81 } 82 } 83 } else { 84 for (int i = 0; i < reps; ++i) { 85 for (int ch = 0; ch < 65536; ++ch) { 86 Character.digit((int) chars[ch], 10); 87 } 88 } 89 } 90 } 91 timeGetNumericValue(int reps)92 public void timeGetNumericValue(int reps) { 93 if (overload == Overload.CHAR) { 94 for (int i = 0; i < reps; ++i) { 95 for (int ch = 0; ch < 65536; ++ch) { 96 Character.getNumericValue(chars[ch]); 97 } 98 } 99 } else { 100 for (int i = 0; i < reps; ++i) { 101 for (int ch = 0; ch < 65536; ++ch) { 102 Character.getNumericValue((int) chars[ch]); 103 } 104 } 105 } 106 } 107 timeIsDigit(int reps)108 public void timeIsDigit(int reps) { 109 if (overload == Overload.CHAR) { 110 for (int i = 0; i < reps; ++i) { 111 for (int ch = 0; ch < 65536; ++ch) { 112 Character.isDigit(chars[ch]); 113 } 114 } 115 } else { 116 for (int i = 0; i < reps; ++i) { 117 for (int ch = 0; ch < 65536; ++ch) { 118 Character.isDigit((int) chars[ch]); 119 } 120 } 121 } 122 } 123 timeIsIdentifierIgnorable(int reps)124 public void timeIsIdentifierIgnorable(int reps) { 125 if (overload == Overload.CHAR) { 126 for (int i = 0; i < reps; ++i) { 127 for (int ch = 0; ch < 65536; ++ch) { 128 Character.isIdentifierIgnorable(chars[ch]); 129 } 130 } 131 } else { 132 for (int i = 0; i < reps; ++i) { 133 for (int ch = 0; ch < 65536; ++ch) { 134 Character.isIdentifierIgnorable((int) chars[ch]); 135 } 136 } 137 } 138 } 139 timeIsJavaIdentifierPart(int reps)140 public void timeIsJavaIdentifierPart(int reps) { 141 if (overload == Overload.CHAR) { 142 for (int i = 0; i < reps; ++i) { 143 for (int ch = 0; ch < 65536; ++ch) { 144 Character.isJavaIdentifierPart(chars[ch]); 145 } 146 } 147 } else { 148 for (int i = 0; i < reps; ++i) { 149 for (int ch = 0; ch < 65536; ++ch) { 150 Character.isJavaIdentifierPart((int) chars[ch]); 151 } 152 } 153 } 154 } 155 timeIsJavaIdentifierStart(int reps)156 public void timeIsJavaIdentifierStart(int reps) { 157 if (overload == Overload.CHAR) { 158 for (int i = 0; i < reps; ++i) { 159 for (int ch = 0; ch < 65536; ++ch) { 160 Character.isJavaIdentifierStart(chars[ch]); 161 } 162 } 163 } else { 164 for (int i = 0; i < reps; ++i) { 165 for (int ch = 0; ch < 65536; ++ch) { 166 Character.isJavaIdentifierStart((int) chars[ch]); 167 } 168 } 169 } 170 } 171 timeIsLetter(int reps)172 public void timeIsLetter(int reps) { 173 if (overload == Overload.CHAR) { 174 for (int i = 0; i < reps; ++i) { 175 for (int ch = 0; ch < 65536; ++ch) { 176 Character.isLetter(chars[ch]); 177 } 178 } 179 } else { 180 for (int i = 0; i < reps; ++i) { 181 for (int ch = 0; ch < 65536; ++ch) { 182 Character.isLetter((int) chars[ch]); 183 } 184 } 185 } 186 } 187 timeIsLetterOrDigit(int reps)188 public void timeIsLetterOrDigit(int reps) { 189 if (overload == Overload.CHAR) { 190 for (int i = 0; i < reps; ++i) { 191 for (int ch = 0; ch < 65536; ++ch) { 192 Character.isLetterOrDigit(chars[ch]); 193 } 194 } 195 } else { 196 for (int i = 0; i < reps; ++i) { 197 for (int ch = 0; ch < 65536; ++ch) { 198 Character.isLetterOrDigit((int) chars[ch]); 199 } 200 } 201 } 202 } 203 timeIsLowerCase(int reps)204 public void timeIsLowerCase(int reps) { 205 if (overload == Overload.CHAR) { 206 for (int i = 0; i < reps; ++i) { 207 for (int ch = 0; ch < 65536; ++ch) { 208 Character.isLowerCase(chars[ch]); 209 } 210 } 211 } else { 212 for (int i = 0; i < reps; ++i) { 213 for (int ch = 0; ch < 65536; ++ch) { 214 Character.isLowerCase((int) chars[ch]); 215 } 216 } 217 } 218 } 219 timeIsSpaceChar(int reps)220 public void timeIsSpaceChar(int reps) { 221 if (overload == Overload.CHAR) { 222 for (int i = 0; i < reps; ++i) { 223 for (int ch = 0; ch < 65536; ++ch) { 224 Character.isSpaceChar(chars[ch]); 225 } 226 } 227 } else { 228 for (int i = 0; i < reps; ++i) { 229 for (int ch = 0; ch < 65536; ++ch) { 230 Character.isSpaceChar((int) chars[ch]); 231 } 232 } 233 } 234 } 235 timeIsUpperCase(int reps)236 public void timeIsUpperCase(int reps) { 237 if (overload == Overload.CHAR) { 238 for (int i = 0; i < reps; ++i) { 239 for (int ch = 0; ch < 65536; ++ch) { 240 Character.isUpperCase(chars[ch]); 241 } 242 } 243 } else { 244 for (int i = 0; i < reps; ++i) { 245 for (int ch = 0; ch < 65536; ++ch) { 246 Character.isUpperCase((int) chars[ch]); 247 } 248 } 249 } 250 } 251 timeIsWhitespace(int reps)252 public void timeIsWhitespace(int reps) { 253 if (overload == Overload.CHAR) { 254 for (int i = 0; i < reps; ++i) { 255 for (int ch = 0; ch < 65536; ++ch) { 256 Character.isWhitespace(chars[ch]); 257 } 258 } 259 } else { 260 for (int i = 0; i < reps; ++i) { 261 for (int ch = 0; ch < 65536; ++ch) { 262 Character.isWhitespace((int) chars[ch]); 263 } 264 } 265 } 266 } 267 timeToLowerCase(int reps)268 public void timeToLowerCase(int reps) { 269 if (overload == Overload.CHAR) { 270 for (int i = 0; i < reps; ++i) { 271 for (int ch = 0; ch < 65536; ++ch) { 272 Character.toLowerCase(chars[ch]); 273 } 274 } 275 } else { 276 for (int i = 0; i < reps; ++i) { 277 for (int ch = 0; ch < 65536; ++ch) { 278 Character.toLowerCase((int) chars[ch]); 279 } 280 } 281 } 282 } 283 timeToUpperCase(int reps)284 public void timeToUpperCase(int reps) { 285 if (overload == Overload.CHAR) { 286 for (int i = 0; i < reps; ++i) { 287 for (int ch = 0; ch < 65536; ++ch) { 288 Character.toUpperCase(chars[ch]); 289 } 290 } 291 } else { 292 for (int i = 0; i < reps; ++i) { 293 for (int ch = 0; ch < 65536; ++ch) { 294 Character.toUpperCase((int) chars[ch]); 295 } 296 } 297 } 298 } 299 } 300