markuszeller

Benchmark SQRT vs EXP 0.5

May 2nd, 2021 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1. <?php
  2. $times = [];
  3. for ($x = 0; $x < 10; $x++) {
  4.     $start = microtime(true);
  5.     for ($i = 0; $i < 1e7; $i++) {
  6.         $a = sqrt($i);
  7.     }
  8.     $times[$x] = microtime(true) - $start;
  9.     echo 'sqrt: ', $times[$x], PHP_EOL;
  10. }
  11. $avg1 = array_sum($times);
  12. echo 'average: ', $avg1 / 10, PHP_EOL;
  13.  
  14. echo str_repeat('=', 70), PHP_EOL;
  15.  
  16. $times = [];
  17. for ($x = 0; $x < 10; $x++) {
  18.     $start = microtime(true);
  19.     for ($i = 0; $i < 1e7; $i++) {
  20.         $a = $i ** .5;
  21.     }
  22.     $times[$x] = microtime(true) - $start;
  23.     echo 'exp: ', $times[$x], PHP_EOL;
  24. }
  25. $avg2 = array_sum($times);
  26. echo 'average: ', $avg2 / 10, PHP_EOL;
  27.  
  28. echo 'ratio: ', max($avg1, $avg2) / min($avg1, $avg2), PHP_EOL;
  29.  
  30. /**
  31. sqrt: 1.6493890285492
  32. sqrt: 1.6189279556274
  33. sqrt: 1.6098041534424
  34. sqrt: 1.6156868934631
  35. sqrt: 1.6098020076752
  36. sqrt: 1.5989210605621
  37. sqrt: 1.6093671321869
  38. sqrt: 1.604642868042
  39. sqrt: 1.6029407978058
  40. sqrt: 1.6166899204254
  41. average: 1.613617181778
  42. ======================================================================
  43. exp: 0.55803894996643
  44. exp: 0.55601787567139
  45. exp: 0.55864214897156
  46. exp: 0.56141209602356
  47. exp: 0.55727696418762
  48. exp: 0.55723094940186
  49. exp: 0.55681586265564
  50. exp: 0.55726480484009
  51. exp: 0.56148099899292
  52. exp: 0.5568699836731
  53. average: 0.55810506343842
  54. ratio: 2.8912426843732
  55. */
Add Comment
Please, Sign In to add comment