Skip to content

Instantly share code, notes, and snippets.

@rtheunissen
Created October 28, 2018 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtheunissen/3c19d829774464f481f853e624fe058c to your computer and use it in GitHub Desktop.
Save rtheunissen/3c19d829774464f481f853e624fe058c to your computer and use it in GitHub Desktop.
Benchmark between BC Math and Decimal
<?php
use Decimal\Decimal;
/* Number of times to perform each calculation. */
$iterations = 1000000;
/* Candidates */
define("DECIMAL", "decimal");
define("BC_MATH", "bcmath");
/* Values used for each benchmark */
$samples = [
"int" => [
1,
2,
7,
13,
57,
123456789,
987654321,
-123456789,
-987654321,
PHP_INT_MAX,
PHP_INT_MIN,
],
"string" => [
"0.1",
"0.0000123456789",
"12345.6789",
"-98765.4321",
"9.87654321",
"-1.23456789",
"0.00000000123456789",
"-0.00000000123456789",
"12345.6789",
"-12345.6789",
],
];
/* Calculates the runtime of each candidate, prints progress, updates report. */
function benchmark(string $action, array $calculations)
{
global $samples;
global $iterations;
echo "\n\t$action";
echo "\n\t" . str_repeat("-", strlen($action));
foreach ($samples as $type => $values) {
/* Each candidate has a calculation function. */
foreach ($calculations as $name => $calculation) {
echo "\n\t\t$name\ttype: $type\t";
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$calculation($values);
}
$runtime = microtime(true) - $start;
$result = $calculation($values);
/* Display results as we go... */
echo "time: " . round($runtime, 4) . "\t";
echo "result: " . rtrim(rtrim($result, '0'), '.');
}
}
}
/**
* ADD
*/
benchmark("add", [
BC_MATH => function(array $values): string {
$result = "0";
foreach ($values as $value) {
$result = bcadd($result, $value, 50);
}
return $result;
},
DECIMAL => function(array $values): string {
$result = new Decimal("0", 100);
foreach ($values as $value) {
$result = $result + $value;
}
return (string) $result;
},
]);
/**
* SUBTRACT
*/
benchmark("sub", [
BC_MATH => function(array $values): string {
$result = "0";
foreach ($values as $value) {
$result = bcsub($result, $value, 50);
}
return $result;
},
DECIMAL => function(array $values): string {
$result = new Decimal("0", 100);
foreach ($values as $value) {
$result = $result - $value;
}
return (string) $result;
},
]);
/**
* MULTIPLY
*/
benchmark("mul", [
BC_MATH => function(array $values): string {
$result = "1";
foreach ($values as $value) {
$result = bcmul($result, $value, 50);
}
return $result;
},
DECIMAL => function(array $values): string {
$result = new Decimal("1", 100);
foreach ($values as $value) {
$result = $result * $value;
}
return (string) $result;
},
]);
/**
* DIVIDE
*/
benchmark("div", [
BC_MATH => function(array $values): string {
$result = '1';
foreach ($values as $value) {
$result = @bcdiv($result, $value, 50);
}
return (string) $result;
},
DECIMAL => function(array $values): string {
$result = new Decimal('1', 100);
foreach ($values as $value) {
$result = $result / $value;
}
return (string) $result;
},
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment