[php-src] PR #15790: ext/bcmath: bcpow() performance improvement
| From: | jorgsowa | Date: | Sat, 07 Sep 2024 13:08:18 +0000 |
| Subject: | [php-src] PR #15790: ext/bcmath: bcpow() performance improvement | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-28465@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/15790
Author: jorgsowa
This PR improves the performance of the BCMath function
bcpow.
We don't need to perform the usual multiplication when we raise a number to a specific set of
numbers. I modified the content of bc_mulitply and removed all the redundant operations
for the same number.
For small exponents, there is neglectable improvement, but for bigger there is up to 5%. However,
I'm not quite happy with this benchmark and I need to use something else.
Benchmark:
```
<?php
$numbers = ['10', '2.1234567', '2'];
$exponents = [1, 2, 10, 64, 100];
foreach($numbers as $number) {
foreach($exponents as $exponent) {
echo "Number: $number, Exponent: $exponent = ";
$start = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
bcpow($number, $exponent, 20);
}
echo microtime(true) - $start . PHP_EOL;
}
}
```
| Number | Exponent | Before | After | Difference |
| ------------- | ------------- | ----------------- | ----------------- | ------------- |
| 10 | 1 | 0.369961977005 | 0.3674750328064 | -0.67 % |
| 10 | 2 | 0.39027285575867 | 0.38206386566162 | -2.14 % |
| 10 | 10 | 0.73798298835754 | 0.70764207839966 | -4.28 % |
| 10 | 64 | 1.6131219863892 | 1.4904029369354 | -8.23 % |
| 10 | 100 | 2.5796089172363 | 2.4571402072906 | -4.98 % |
| 2.1234567 | 1 | 0.36139988899231 | 0.36435699462891 | +0.82 % |
| 2.1234567 | 2 | 0.448086977005 | 0.44263195991516 | -1.23 % |
| 2.1234567 | 10 | 1.6505491733551 | 1.5602447986603 | -5.78 % |
| 2.1234567 | 64 | 6.6499609947205 | 6.3584640026093 | -4.58 % |
| 2.1234567 | 100 | 14.168872833252 | 13.804186105728 | -2.64 % |
| 2 | 1 | 0.33516597747803 | 0.32968902587891 | -1.66 % |
| 2 | 2 | 0.38029599189758 | 0.36791205406189 | -3.36 % |
| 2 | 10 | 0.59442806243896 | 0.5603358745575 | -6.08 % |
| 2 | 64 | 0.94932103157043 | 0.86743497848511 | -9.44 % |
| 2 | 100 | 1.4967222213745 | 1.39244389534 | -7.48 % |