/benchmarks.php

https://github.com/php-decimal/ext-decimal · PHP · 202 lines · 104 code · 34 blank · 64 comment · 1 complexity · 200746ea445daed44e239c333623020a MD5 · raw file

  1. <?php
  2. use Decimal\Decimal;
  3. use Decimal\Rational;
  4. ini_set("memory_limit", -1);
  5. /* Number of times to perform each calculation. */
  6. $iterations = 100000;
  7. /* Candidates */
  8. define("DECIMAL", "decimal");
  9. define("RATIONAL", "rational");
  10. define("BC_MATH", "bcmath");
  11. /* Values used for each benchmark */
  12. $samples = [
  13. "int" => [
  14. 1,
  15. 2,
  16. 7,
  17. 13,
  18. 57,
  19. 123456789,
  20. 987654321,
  21. -123456789,
  22. -987654321,
  23. PHP_INT_MAX,
  24. PHP_INT_MIN,
  25. ],
  26. "string" => [
  27. "0.1",
  28. "0.0000123456789",
  29. "12345.6789",
  30. "-98765.4321",
  31. "9.87654321",
  32. "-1.23456789",
  33. "0.00000000123456789",
  34. "-0.00000000123456789",
  35. "12345.6789",
  36. "-12345.6789",
  37. ],
  38. ];
  39. /* Calculates the runtime of each candidate, prints progress, updates report. */
  40. function benchmark(string $action, array $calculations)
  41. {
  42. global $samples;
  43. global $iterations;
  44. echo "\n\t$action";
  45. echo "\n\t" . str_repeat("-", strlen($action));
  46. foreach ($samples as $type => $values) {
  47. /* Each candidate has a calculation function. */
  48. foreach ($calculations as $name => $calculation) {
  49. $name = str_pad($name, 10, " ");
  50. $type = str_pad($type, 10, " ");
  51. echo "\n\t\t$name\ttype: $type\t";
  52. $start = microtime(true);
  53. for ($i = 0; $i < $iterations; $i++) {
  54. $calculation($values);
  55. }
  56. $runtime = microtime(true) - $start;
  57. $result = $calculation($values);
  58. /* Display results as we go... */
  59. echo "time: " . number_format($runtime, 4) . "\t";
  60. echo "result: " . $result;
  61. }
  62. echo "\n";
  63. }
  64. }
  65. /**
  66. * ADD
  67. */
  68. benchmark("add", [
  69. BC_MATH => function(array $values): string {
  70. $result = "0";
  71. foreach ($values as $value) {
  72. $result = bcadd($result, $value, 50);
  73. }
  74. return $result;
  75. },
  76. DECIMAL => function(array $values): string {
  77. $result = Decimal::valueOf("0", 100);
  78. foreach ($values as $value) {
  79. $result += $value;
  80. }
  81. return (string) $result;
  82. },
  83. RATIONAL => function(array $values): string {
  84. $result = Rational::valueOf("0");
  85. foreach ($values as $value) {
  86. $result += $value;
  87. }
  88. return (string) $result;
  89. },
  90. ]);
  91. // /**
  92. // * SUBTRACT
  93. // */
  94. // benchmark("sub", [
  95. // // BC_MATH => function(array $values): string {
  96. // // $result = "0";
  97. // // foreach ($values as $value) {
  98. // // $result = bcsub($result, $value, 50);
  99. // // }
  100. // // return $result;
  101. // // },
  102. // // DECIMAL => function(array $values): string {
  103. // // $result = Decimal::valueOf("0", 100);
  104. // // foreach ($values as $value) {
  105. // // $result = $result - $value;
  106. // // }
  107. // // return (string) $result;
  108. // // },
  109. // RATIONAL => function(array $values): string {
  110. // $result = Rational::valueOf("0");
  111. // foreach ($values as $value) {
  112. // $result = $result - $value;
  113. // }
  114. // return (string) $result;
  115. // },
  116. // ]);
  117. // /**
  118. // * MULTIPLY
  119. // */
  120. // benchmark("mul", [
  121. // // BC_MATH => function(array $values): string {
  122. // // $result = "1";
  123. // // foreach ($values as $value) {
  124. // // $result = bcmul($result, $value, 50);
  125. // // }
  126. // // return $result;
  127. // // },
  128. // // DECIMAL => function(array $values): string {
  129. // // $result = Decimal::valueOf("1", 100);
  130. // // foreach ($values as $value) {
  131. // // $result = $result * $value;
  132. // // }
  133. // // return (string) $result;
  134. // // },
  135. // RATIONAL => function(array $values): string {
  136. // $result = Rational::valueOf("1");
  137. // foreach ($values as $value) {
  138. // $result = $result * $value;
  139. // }
  140. // return (string) $result;
  141. // },
  142. // ]);
  143. /**
  144. * DIVIDE
  145. */
  146. benchmark("div", [
  147. BC_MATH => function(array $values): string {
  148. $result = '1';
  149. foreach ($values as $value) {
  150. $result = @bcdiv($result, $value, 50);
  151. }
  152. return (string) $result;
  153. },
  154. DECIMAL => function(array $values): string {
  155. $result = Decimal::valueOf('1', 100);
  156. foreach ($values as $value) {
  157. $result /= $value;
  158. }
  159. return (string) $result;
  160. },
  161. RATIONAL => function(array $values): string {
  162. $result = Rational::valueOf("1");
  163. foreach ($values as $value) {
  164. $result /= $value;
  165. }
  166. return (string) $result;
  167. },
  168. ]);