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