[php-src] PR #14376: ext/bcmath: Use divide and conquer method in `bcmul`
| From: | SakiTakamachi | Date: | Thu, 30 May 2024 15:26:22 +0000 |
| Subject: | [php-src] PR #14376: ext/bcmath: Use divide and conquer method in `bcmul` | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-26393@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/14376
Author: SakiTakamachi
It does not significantly affect the speed of computations that do not use divide and conquer,
making computations with large numbers of digits faster.
I was wondering if I could make the commits smaller somehow, but since all changes depend on each
other, I couldn't separate the commits...
## Benchmarks
1:
```
$num1 = '1.2345678';
$num2 = '2.1234567';
for ($i = 0; $i < 5000000; $i++) {
bcmul($num1, $num2, 7);
}
```
2:
```
$num1 = '1.2345678901234567890';
$num2 = '2.12345678901234567890';
for ($i = 0; $i < 3000000; $i++) {
bcmul($num1, $num2, 20);
}
```
3:
```
$num1 = str_repeat('1234567890', 300);
$num2 = str_repeat('9876543210', 300);
for ($i = 0; $i < 6000; $i++) {
bcmul($num1, $num2, 0);
}
```
4:
```
$num1 = str_repeat('1234567890', 1024);
$num2 = str_repeat('9876543210', 1024);
for ($i = 0; $i < 500; $i++) {
bcmul($num1, $num2, 0);
}
```
## before
```
# hyperfine "php /bc/mul/1.php" --warmup 10
Benchmark 1: php /bc/mul/1.php
Time (mean ± σ): 611.1 ms ± 3.3 ms [User: 605.5 ms, System: 4.8 ms]
Range (min … max): 607.7 ms … 619.1 ms 10 runs
# hyperfine "php /bc/mul/2.php" --warmup 10
Benchmark 1: php /bc/mul/2.php
Time (mean ± σ): 507.5 ms ± 4.4 ms [User: 503.1 ms, System: 3.5 ms]
Range (min … max): 503.3 ms … 518.3 ms 10 runs
# hyperfine "php /bc/mul/3.php" --warmup 10
Benchmark 1: php /bc/mul/3.php
Time (mean ± σ): 537.0 ms ± 7.3 ms [User: 533.4 ms, System: 2.8 ms]
Range (min … max): 528.3 ms … 549.0 ms 10 runs
# hyperfine "php /bc/mul/4.php" --warmup 10
Benchmark 1: php /bc/mul/4.php
Time (mean ± σ): 476.2 ms ± 5.0 ms [User: 471.6 ms, System: 3.8 ms]
Range (min … max): 470.7 ms … 487.4 ms 10 runs
```
## after
```
# hyperfine "php /bc/mul/1.php" --warmup 10
Benchmark 1: php /bc/mul/1.php
Time (mean ± σ): 610.4 ms ± 8.1 ms [User: 606.1 ms, System: 2.5 ms]
Range (min … max): 601.6 ms … 628.6 ms 10 runs
# hyperfine "php /bc/mul/2.php" --warmup 10
Benchmark 1: php /bc/mul/2.php
Time (mean ± σ): 502.7 ms ± 4.4 ms [User: 498.6 ms, System: 3.2 ms]
Range (min … max): 496.4 ms … 509.2 ms 10 runs
# hyperfine "php /bc/mul/3.php" --warmup 10
Benchmark 1: php /bc/mul/3.php
Time (mean ± σ): 450.4 ms ± 14.3 ms [User: 446.5 ms, System: 3.1 ms]
Range (min … max): 433.8 ms … 474.1 ms 10 runs
# hyperfine "php /bc/mul/4.php" --warmup 10
Benchmark 1: php /bc/mul/4.php
Time (mean ± σ): 292.2 ms ± 6.9 ms [User: 287.3 ms, System: 4.1 ms]
Range (min … max): 285.7 ms … 307.3 ms 10 runs
```