Skip to content

Instantly share code, notes, and snippets.

@rtheunissen
Created September 29, 2018 17:07
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/973ea1719c02a4204dabfe9dffd78c4b to your computer and use it in GitHub Desktop.
Save rtheunissen/973ea1719c02a4204dabfe9dffd78c4b to your computer and use it in GitHub Desktop.
Basic benchmark for gmpi vs decimal, using bcmath as reference
<?php
ini_set('memory_limit', -1);
srand(0);
$length = 100000;
$maxInt = 10 ** 10;
$values = [];
for ($i = 0; $i < $length; $i++) {
$int = (rand() % $maxInt);
$dec = (rand() % $maxInt);
$values[] = 0;
$values[] = 1;
$values[] = 0.5;
$values[] = 1.5;
$values[] = '0.5';
$values[] = '1.5';
$values[] = "$int.$dec";
$values[] = (int) ("$int.$dec");
$values[] = (float) ("$int.$dec");
}
$benchmark = function($name, $obj) use ($values) {
$time = microtime(true);
foreach ($values as $value) {
$obj = $obj + $value;
$obj = $obj + $value;
$obj = $obj - $value;
}
$time = round(microtime(true) - $time, 4);
print_r([
'title' => $name,
'taken' => $time,
'value' => (string) $obj,
]);
};
$reference = function() use ($values) {
$res = 0;
foreach ($values as $value) {
$res = bcadd($res, $value, 20);
$res = bcadd($res, $value, 20);
$res = bcsub($res, $value, 20);
}
print_r([
'title' => 'bcmath',
'taken' => '-',
'value' => (string) $res,
]);
};
ini_set('decimal.precision', 200);
ini_set('gmpi.precision', 200);
$benchmark('gmpi', new \GMPi\Float(0));
$benchmark('decimal', new \Decimal\Decimal(0));
$reference();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment