From 2059332b72e6b4912633de08a9ce3f9d44a73980 Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Thu, 6 Apr 2023 13:47:58 +0200 Subject: [PATCH 1/4] Fix wrong method calls in MoneyOperationBench --- benchmark/MoneyOperationBench.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/MoneyOperationBench.php b/benchmark/MoneyOperationBench.php index 4fe5d277..34cf22cf 100644 --- a/benchmark/MoneyOperationBench.php +++ b/benchmark/MoneyOperationBench.php @@ -57,12 +57,12 @@ public function benchMin(): void public function benchMax(): void { - Money::min($this->a, $this->b, $this->a, $this->b); + Money::max($this->a, $this->b, $this->a, $this->b); } public function benchAvg(): void { - Money::min($this->a, $this->b, $this->a, $this->b); + Money::avg($this->a, $this->b, $this->a, $this->b); } public function benchRatioOf(): void From 6a862521b2f0f3d0395440569e544b5ecad4fca8 Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Thu, 6 Apr 2023 13:48:17 +0200 Subject: [PATCH 2/4] Introduce calculator benchmarks --- .../Calculator/BcMathCalculatorBench.php | 15 ++++ benchmark/Calculator/CalculatorBench.php | 77 +++++++++++++++++++ benchmark/Calculator/GmpCalculatorBench.php | 15 ++++ 3 files changed, 107 insertions(+) create mode 100644 benchmark/Calculator/BcMathCalculatorBench.php create mode 100644 benchmark/Calculator/CalculatorBench.php create mode 100644 benchmark/Calculator/GmpCalculatorBench.php diff --git a/benchmark/Calculator/BcMathCalculatorBench.php b/benchmark/Calculator/BcMathCalculatorBench.php new file mode 100644 index 00000000..cbb80ded --- /dev/null +++ b/benchmark/Calculator/BcMathCalculatorBench.php @@ -0,0 +1,15 @@ + + */ + abstract protected function getCalculator(): string; + + public function benchCompare(): void + { + $this->getCalculator()::compare('1', '1'); + $this->getCalculator()::compare('1', '5'); + $this->getCalculator()::compare('5', '5'); + $this->getCalculator()::compare('5.5', '1.5'); + $this->getCalculator()::compare('1.5', '5.5'); + } + + public function benchAdd(): void + { + $this->getCalculator()::add('1', '5'); + } + + public function benchSubtract(): void + { + $this->getCalculator()::subtract('1', '5'); + } + + public function benchMultiply(): void + { + $this->getCalculator()::multiply('5', '25'); + $this->getCalculator()::multiply('5', '1.5'); + } + + public function benchDivide(): void + { + $this->getCalculator()::divide('5', '4'); + } + + public function benchCeil(): void + { + $this->getCalculator()::ceil('5.5'); + } + + public function benchFloor(): void + { + $this->getCalculator()::floor('5.5'); + } + + public function benchAbsolute(): void + { + $this->getCalculator()::absolute('5'); + $this->getCalculator()::absolute('-5'); + } + + public function benchRound(): void + { + $this->getCalculator()::round('2.6', Money::ROUND_HALF_EVEN); + } + + public function benchShare(): void + { + $this->getCalculator()::share('10', '2', '4'); + } + + public function benchMod(): void + { + $this->getCalculator()::mod('11', '5'); + } +} diff --git a/benchmark/Calculator/GmpCalculatorBench.php b/benchmark/Calculator/GmpCalculatorBench.php new file mode 100644 index 00000000..a026f28c --- /dev/null +++ b/benchmark/Calculator/GmpCalculatorBench.php @@ -0,0 +1,15 @@ + Date: Thu, 6 Apr 2023 13:48:24 +0200 Subject: [PATCH 3/4] Optimize result representation for BcMathCalculator::add and BcMathCalculator::substract --- src/Calculator/BcMathCalculator.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Calculator/BcMathCalculator.php b/src/Calculator/BcMathCalculator.php index 0640c883..0f338e53 100644 --- a/src/Calculator/BcMathCalculator.php +++ b/src/Calculator/BcMathCalculator.php @@ -17,6 +17,7 @@ use function bcmul; use function bcsub; use function ltrim; +use function str_contains; final class BcMathCalculator implements Calculator { @@ -31,13 +32,17 @@ public static function compare(string $a, string $b): int /** @psalm-pure */ public static function add(string $amount, string $addend): string { - return bcadd($amount, $addend, self::SCALE); + $scale = str_contains($amount, '.') || str_contains($addend, '.') ? self::SCALE : 0; + + return bcadd($amount, $addend, $scale); } /** @psalm-pure */ public static function subtract(string $amount, string $subtrahend): string { - return bcsub($amount, $subtrahend, self::SCALE); + $scale = str_contains($amount, '.') || str_contains($subtrahend, '.') ? self::SCALE : 0; + + return bcsub($amount, $subtrahend, $scale); } /** @psalm-pure */ From 714db870af881026dab271f398c98020a196d330 Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Tue, 11 Apr 2023 07:19:39 +0200 Subject: [PATCH 4/4] Improve decimal detection --- src/Calculator/BcMathCalculator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Calculator/BcMathCalculator.php b/src/Calculator/BcMathCalculator.php index 0f338e53..087b3c05 100644 --- a/src/Calculator/BcMathCalculator.php +++ b/src/Calculator/BcMathCalculator.php @@ -32,7 +32,7 @@ public static function compare(string $a, string $b): int /** @psalm-pure */ public static function add(string $amount, string $addend): string { - $scale = str_contains($amount, '.') || str_contains($addend, '.') ? self::SCALE : 0; + $scale = str_contains($amount . $addend, '.') ? self::SCALE : 0; return bcadd($amount, $addend, $scale); } @@ -40,7 +40,7 @@ public static function add(string $amount, string $addend): string /** @psalm-pure */ public static function subtract(string $amount, string $subtrahend): string { - $scale = str_contains($amount, '.') || str_contains($subtrahend, '.') ? self::SCALE : 0; + $scale = str_contains($amount . $subtrahend, '.') ? self::SCALE : 0; return bcsub($amount, $subtrahend, $scale); }