From f47bc24b2cccef74872a430d44ac921fcde2d557 Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Thu, 6 Apr 2023 11:04:32 +0200 Subject: [PATCH 1/4] Introduce PerformanceOptimizedDelegateCalculator --- ...PerformanceOptimizedDelegateCalculator.php | 87 +++++++++++++++++++ ...ormanceOptimizedDelegateCalculatorTest.php | 24 +++++ 2 files changed, 111 insertions(+) create mode 100644 src/Calculator/PerformanceOptimizedDelegateCalculator.php create mode 100644 tests/Calculator/PerformanceOptimizedDelegateCalculatorTest.php diff --git a/src/Calculator/PerformanceOptimizedDelegateCalculator.php b/src/Calculator/PerformanceOptimizedDelegateCalculator.php new file mode 100644 index 00000000..bc9f91e4 --- /dev/null +++ b/src/Calculator/PerformanceOptimizedDelegateCalculator.php @@ -0,0 +1,87 @@ + + */ + protected function getCalculator(): string + { + return PerformanceOptimizedDelegateCalculator::class; + } +} From fdfd57c0e10ac81f540cca156b0a81db25236cf0 Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Thu, 6 Apr 2023 11:43:14 +0200 Subject: [PATCH 2/4] Add phpbench stubs for calculators --- .../Calculator/BcMathCalculatorBench.php | 15 ++++ benchmark/Calculator/CalculatorBench.php | 77 +++++++++++++++++++ benchmark/Calculator/GmpCalculatorBench.php | 15 ++++ ...rmanceOptimizedDelegateCalculatorBench.php | 15 ++++ benchmark/MoneyOperationBench.php | 4 +- phpbench.json | 7 +- 6 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 benchmark/Calculator/BcMathCalculatorBench.php create mode 100644 benchmark/Calculator/CalculatorBench.php create mode 100644 benchmark/Calculator/GmpCalculatorBench.php create mode 100644 benchmark/Calculator/PerformanceOptimizedDelegateCalculatorBench.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 @@ +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 diff --git a/phpbench.json b/phpbench.json index 2b307866..a6615b16 100644 --- a/phpbench.json +++ b/phpbench.json @@ -1,4 +1,9 @@ { "runner.bootstrap": "vendor/autoload.php", - "runner.path": "benchmark" + "runner.path": "benchmark", + "runner.php_config": + { + "opcache.enable": 1, + "opcache.enable_cli": 1 + } } From 46fb6ce7b6f78fce7d3f2e32353b541b0a26e67a Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Thu, 6 Apr 2023 13:26:00 +0200 Subject: [PATCH 3/4] Remove PerformanceOptimizedDelegateCalculator --- ...rmanceOptimizedDelegateCalculatorBench.php | 15 ---- ...PerformanceOptimizedDelegateCalculator.php | 87 ------------------- ...ormanceOptimizedDelegateCalculatorTest.php | 24 ----- 3 files changed, 126 deletions(-) delete mode 100644 benchmark/Calculator/PerformanceOptimizedDelegateCalculatorBench.php delete mode 100644 src/Calculator/PerformanceOptimizedDelegateCalculator.php delete mode 100644 tests/Calculator/PerformanceOptimizedDelegateCalculatorTest.php diff --git a/benchmark/Calculator/PerformanceOptimizedDelegateCalculatorBench.php b/benchmark/Calculator/PerformanceOptimizedDelegateCalculatorBench.php deleted file mode 100644 index 381d37d0..00000000 --- a/benchmark/Calculator/PerformanceOptimizedDelegateCalculatorBench.php +++ /dev/null @@ -1,15 +0,0 @@ - - */ - protected function getCalculator(): string - { - return PerformanceOptimizedDelegateCalculator::class; - } -} From 0cdb07745a4d696f6fd3faf59eaac9e250695254 Mon Sep 17 00:00:00 2001 From: Benjamin Cremer Date: Thu, 6 Apr 2023 13:26:50 +0200 Subject: [PATCH 4/4] Optimize result representation for BcMathCalculator::add and BcMathCalculator::substract --- 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 0640c883..e610f679 100644 --- a/src/Calculator/BcMathCalculator.php +++ b/src/Calculator/BcMathCalculator.php @@ -31,13 +31,13 @@ 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); + return bcadd($amount, $addend, 0); } /** @psalm-pure */ public static function subtract(string $amount, string $subtrahend): string { - return bcsub($amount, $subtrahend, self::SCALE); + return bcsub($amount, $subtrahend, 0); } /** @psalm-pure */