Created
September 29, 2018 17:07
-
-
Save rtheunissen/973ea1719c02a4204dabfe9dffd78c4b to your computer and use it in GitHub Desktop.
Basic benchmark for gmpi vs decimal, using bcmath as reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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