1 /* 2 * Copyright (C) 2010 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.Param; 20 import com.google.caliper.Runner; 21 import com.google.caliper.SimpleBenchmark; 22 23 /** 24 * Many of these tests are bogus in that the cost will vary wildly depending on inputs. 25 * For _my_ current purposes, that's okay. But beware! 26 */ 27 public class MathBenchmark extends SimpleBenchmark { 28 private final double d = 1.2; 29 private final float f = 1.2f; 30 private final int i = 1; 31 private final long l = 1L; 32 33 // NOTE: To avoid the benchmarked function from being optimized away, we store the result 34 // and use it as the benchmark's return value. This is good enough for now but may not be in 35 // the future, a smart compiler could determine that the result value will depend on whether 36 // we get into the loop or not and turn the whole loop into an if statement. 37 timeAbsD(int reps)38 public double timeAbsD(int reps) { 39 double result = d; 40 for (int rep = 0; rep < reps; ++rep) { 41 result = Math.abs(d); 42 } 43 return result; 44 } 45 timeAbsF(int reps)46 public float timeAbsF(int reps) { 47 float result = f; 48 for (int rep = 0; rep < reps; ++rep) { 49 result = Math.abs(f); 50 } 51 return result; 52 } 53 timeAbsI(int reps)54 public int timeAbsI(int reps) { 55 int result = i; 56 for (int rep = 0; rep < reps; ++rep) { 57 result = Math.abs(i); 58 } 59 return result; 60 } 61 timeAbsL(int reps)62 public long timeAbsL(int reps) { 63 long result = l; 64 for (int rep = 0; rep < reps; ++rep) { 65 result = Math.abs(l); 66 } 67 return result; 68 } 69 timeAcos(int reps)70 public double timeAcos(int reps) { 71 double result = d; 72 for (int rep = 0; rep < reps; ++rep) { 73 result = Math.acos(d); 74 } 75 return result; 76 } 77 timeAsin(int reps)78 public double timeAsin(int reps) { 79 double result = d; 80 for (int rep = 0; rep < reps; ++rep) { 81 result = Math.asin(d); 82 } 83 return result; 84 } 85 timeAtan(int reps)86 public double timeAtan(int reps) { 87 double result = d; 88 for (int rep = 0; rep < reps; ++rep) { 89 result = Math.atan(d); 90 } 91 return result; 92 } 93 timeAtan2(int reps)94 public double timeAtan2(int reps) { 95 double result = d; 96 for (int rep = 0; rep < reps; ++rep) { 97 result = Math.atan2(3, 4); 98 } 99 return result; 100 } 101 timeCbrt(int reps)102 public double timeCbrt(int reps) { 103 double result = d; 104 for (int rep = 0; rep < reps; ++rep) { 105 result = Math.cbrt(d); 106 } 107 return result; 108 } 109 timeCeil(int reps)110 public double timeCeil(int reps) { 111 double result = d; 112 for (int rep = 0; rep < reps; ++rep) { 113 result = Math.ceil(d); 114 } 115 return result; 116 } 117 timeCopySignD(int reps)118 public double timeCopySignD(int reps) { 119 double result = d; 120 for (int rep = 0; rep < reps; ++rep) { 121 result = Math.copySign(d, d); 122 } 123 return result; 124 } 125 timeCopySignF(int reps)126 public float timeCopySignF(int reps) { 127 float result = f; 128 for (int rep = 0; rep < reps; ++rep) { 129 result = Math.copySign(f, f); 130 } 131 return result; 132 } 133 timeCopySignD_strict(int reps)134 public double timeCopySignD_strict(int reps) { 135 double result = d; 136 for (int rep = 0; rep < reps; ++rep) { 137 result = StrictMath.copySign(d, d); 138 } 139 return result; 140 } 141 timeCopySignF_strict(int reps)142 public float timeCopySignF_strict(int reps) { 143 float result = f; 144 for (int rep = 0; rep < reps; ++rep) { 145 result = StrictMath.copySign(f, f); 146 } 147 return result; 148 } 149 timeCos(int reps)150 public double timeCos(int reps) { 151 double result = d; 152 for (int rep = 0; rep < reps; ++rep) { 153 result = Math.cos(d); 154 } 155 return result; 156 } 157 timeCosh(int reps)158 public double timeCosh(int reps) { 159 double result = d; 160 for (int rep = 0; rep < reps; ++rep) { 161 result = Math.cosh(d); 162 } 163 return result; 164 } 165 timeExp(int reps)166 public double timeExp(int reps) { 167 double result = d; 168 for (int rep = 0; rep < reps; ++rep) { 169 result = Math.exp(d); 170 } 171 return result; 172 } 173 timeExpm1(int reps)174 public double timeExpm1(int reps) { 175 double result = d; 176 for (int rep = 0; rep < reps; ++rep) { 177 result = Math.expm1(d); 178 } 179 return result; 180 } 181 timeFloor(int reps)182 public double timeFloor(int reps) { 183 double result = d; 184 for (int rep = 0; rep < reps; ++rep) { 185 result = Math.floor(d); 186 } 187 return result; 188 } 189 timeGetExponentD(int reps)190 public int timeGetExponentD(int reps) { 191 int result = i; 192 for (int rep = 0; rep < reps; ++rep) { 193 result = Math.getExponent(d); 194 } 195 return result; 196 } 197 timeGetExponentF(int reps)198 public int timeGetExponentF(int reps) { 199 int result = i; 200 for (int rep = 0; rep < reps; ++rep) { 201 result = Math.getExponent(f); 202 } 203 return result; 204 } 205 timeHypot(int reps)206 public double timeHypot(int reps) { 207 double result = d; 208 for (int rep = 0; rep < reps; ++rep) { 209 result = Math.hypot(d, d); 210 } 211 return result; 212 } 213 timeIEEEremainder(int reps)214 public double timeIEEEremainder(int reps) { 215 double result = d; 216 for (int rep = 0; rep < reps; ++rep) { 217 result = Math.IEEEremainder(d, d); 218 } 219 return result; 220 } 221 timeLog(int reps)222 public double timeLog(int reps) { 223 double result = d; 224 for (int rep = 0; rep < reps; ++rep) { 225 result = Math.log(d); 226 } 227 return result; 228 } 229 timeLog10(int reps)230 public double timeLog10(int reps) { 231 double result = d; 232 for (int rep = 0; rep < reps; ++rep) { 233 result = Math.log10(d); 234 } 235 return result; 236 } 237 timeLog1p(int reps)238 public double timeLog1p(int reps) { 239 double result = d; 240 for (int rep = 0; rep < reps; ++rep) { 241 result = Math.log1p(d); 242 } 243 return result; 244 } 245 timeMaxD(int reps)246 public double timeMaxD(int reps) { 247 double result = d; 248 for (int rep = 0; rep < reps; ++rep) { 249 result = Math.max(d, d); 250 } 251 return result; 252 } 253 timeMaxF(int reps)254 public float timeMaxF(int reps) { 255 float result = f; 256 for (int rep = 0; rep < reps; ++rep) { 257 result = Math.max(f, f); 258 } 259 return result; 260 } 261 timeMaxI(int reps)262 public int timeMaxI(int reps) { 263 int result = i; 264 for (int rep = 0; rep < reps; ++rep) { 265 result = Math.max(i, i); 266 } 267 return result; 268 } 269 timeMaxL(int reps)270 public long timeMaxL(int reps) { 271 long result = l; 272 for (int rep = 0; rep < reps; ++rep) { 273 result = Math.max(l, l); 274 } 275 return result; 276 } 277 timeMinD(int reps)278 public double timeMinD(int reps) { 279 double result = d; 280 for (int rep = 0; rep < reps; ++rep) { 281 result = Math.min(d, d); 282 } 283 return result; 284 } 285 timeMinF(int reps)286 public float timeMinF(int reps) { 287 float result = f; 288 for (int rep = 0; rep < reps; ++rep) { 289 result = Math.min(f, f); 290 } 291 return result; 292 } 293 timeMinI(int reps)294 public int timeMinI(int reps) { 295 int result = i; 296 for (int rep = 0; rep < reps; ++rep) { 297 result = Math.min(i, i); 298 } 299 return result; 300 } 301 timeMinL(int reps)302 public long timeMinL(int reps) { 303 long result = l; 304 for (int rep = 0; rep < reps; ++rep) { 305 result = Math.min(l, l); 306 } 307 return result; 308 } 309 timeNextAfterD(int reps)310 public double timeNextAfterD(int reps) { 311 double result = d; 312 for (int rep = 0; rep < reps; ++rep) { 313 result = Math.nextAfter(d, d); 314 } 315 return result; 316 } 317 timeNextAfterF(int reps)318 public float timeNextAfterF(int reps) { 319 float result = f; 320 for (int rep = 0; rep < reps; ++rep) { 321 result = Math.nextAfter(f, f); 322 } 323 return result; 324 } 325 timeNextUpD(int reps)326 public double timeNextUpD(int reps) { 327 double result = d; 328 for (int rep = 0; rep < reps; ++rep) { 329 result = Math.nextUp(d); 330 } 331 return result; 332 } 333 timeNextUpF(int reps)334 public float timeNextUpF(int reps) { 335 float result = f; 336 for (int rep = 0; rep < reps; ++rep) { 337 result = Math.nextUp(f); 338 } 339 return result; 340 } 341 timePow(int reps)342 public double timePow(int reps) { 343 double result = d; 344 for (int rep = 0; rep < reps; ++rep) { 345 result = Math.pow(d, d); 346 } 347 return result; 348 } 349 timeRandom(int reps)350 public double timeRandom(int reps) { 351 double result = d; 352 for (int rep = 0; rep < reps; ++rep) { 353 result = Math.random(); 354 } 355 return result; 356 } 357 timeRint(int reps)358 public double timeRint(int reps) { 359 double result = d; 360 for (int rep = 0; rep < reps; ++rep) { 361 result = Math.rint(d); 362 } 363 return result; 364 } 365 timeRoundD(int reps)366 public long timeRoundD(int reps) { 367 long result = l; 368 for (int rep = 0; rep < reps; ++rep) { 369 result = Math.round(d); 370 } 371 return result; 372 } 373 timeRoundF(int reps)374 public int timeRoundF(int reps) { 375 int result = i; 376 for (int rep = 0; rep < reps; ++rep) { 377 result = Math.round(f); 378 } 379 return result; 380 } 381 timeScalbD(int reps)382 public double timeScalbD(int reps) { 383 double result = d; 384 for (int rep = 0; rep < reps; ++rep) { 385 result = Math.scalb(d, 5); 386 } 387 return result; 388 } 389 timeScalbF(int reps)390 public float timeScalbF(int reps) { 391 float result = f; 392 for (int rep = 0; rep < reps; ++rep) { 393 result = Math.scalb(f, 5); 394 } 395 return result; 396 } 397 timeSignumD(int reps)398 public double timeSignumD(int reps) { 399 double result = d; 400 for (int rep = 0; rep < reps; ++rep) { 401 result = Math.signum(d); 402 } 403 return result; 404 } 405 timeSignumF(int reps)406 public float timeSignumF(int reps) { 407 float result = f; 408 for (int rep = 0; rep < reps; ++rep) { 409 result = Math.signum(f); 410 } 411 return result; 412 } 413 timeSin(int reps)414 public double timeSin(int reps) { 415 double result = d; 416 for (int rep = 0; rep < reps; ++rep) { 417 result = Math.sin(d); 418 } 419 return result; 420 } 421 timeSinh(int reps)422 public double timeSinh(int reps) { 423 double result = d; 424 for (int rep = 0; rep < reps; ++rep) { 425 result = Math.sinh(d); 426 } 427 return result; 428 } 429 timeSqrt(int reps)430 public double timeSqrt(int reps) { 431 double result = d; 432 for (int rep = 0; rep < reps; ++rep) { 433 result = Math.sqrt(d); 434 } 435 return result; 436 } 437 timeTan(int reps)438 public double timeTan(int reps) { 439 double result = d; 440 for (int rep = 0; rep < reps; ++rep) { 441 result = Math.tan(d); 442 } 443 return result; 444 } 445 timeTanh(int reps)446 public double timeTanh(int reps) { 447 double result = d; 448 for (int rep = 0; rep < reps; ++rep) { 449 result = Math.tanh(d); 450 } 451 return result; 452 } 453 timeToDegrees(int reps)454 public double timeToDegrees(int reps) { 455 double result = d; 456 for (int rep = 0; rep < reps; ++rep) { 457 result = Math.toDegrees(d); 458 } 459 return result; 460 } 461 timeToRadians(int reps)462 public double timeToRadians(int reps) { 463 double result = d; 464 for (int rep = 0; rep < reps; ++rep) { 465 result = Math.toRadians(d); 466 } 467 return result; 468 } 469 timeUlpD(int reps)470 public double timeUlpD(int reps) { 471 double result = d; 472 for (int rep = 0; rep < reps; ++rep) { 473 result = Math.ulp(d); 474 } 475 return result; 476 } 477 timeUlpF(int reps)478 public float timeUlpF(int reps) { 479 float result = f; 480 for (int rep = 0; rep < reps; ++rep) { 481 result = Math.ulp(f); 482 } 483 return result; 484 } 485 } 486