Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext/bcmath: Improving bcpow() performance #18099

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Mar 17, 2025

I ran 500,000 comparisons with PHP 8.3 results. (I would like to perform more comparisons, but PHP 8.3's bcpow() is too heavy and the comparisons never finish.)
No problems were detected.

Benchmarks

The speed difference is especially noticeable when base is less than 1 and there are many leading zeros.
This is because try to eliminate as many zeros as possible during the calculation, and then fill in the missing digits with zeros later.

1:

for ($i = 0; $i < 2000000; $i++) {
    bcpow('123', '4', 0);
}
Benchmark 1: /php-dev/sapi/cli/php /mount/bc/pow/1.php
  Time (mean ± σ):     244.8 ms ±   1.3 ms    [User: 235.5 ms, System: 4.2 ms]
  Range (min … max):   242.8 ms … 247.7 ms    12 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/pow/1.php
  Time (mean ± σ):     279.4 ms ±   3.4 ms    [User: 269.1 ms, System: 5.2 ms]
  Range (min … max):   275.7 ms … 287.4 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/pow/1.php' ran
    1.14 ± 0.02 times faster than '/master/sapi/cli/php /mount/bc/pow/1.php'

2:

for ($i = 0; $i < 200000; $i++) {
    bcpow('123456.789', '64', 10);
}
Benchmark 1: /php-dev/sapi/cli/php /mount/bc/pow/2.php
  Time (mean ± σ):     360.9 ms ±   1.1 ms    [User: 350.9 ms, System: 4.8 ms]
  Range (min … max):   359.4 ms … 363.2 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/pow/2.php
  Time (mean ± σ):     506.4 ms ±   4.3 ms    [User: 496.2 ms, System: 4.7 ms]
  Range (min … max):   501.0 ms … 514.3 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/pow/2.php' ran
    1.40 ± 0.01 times faster than '/master/sapi/cli/php /mount/bc/pow/2.php'

3:

for ($i = 0; $i < 100000; $i++) {
    bcpow('0.00123456', '120', 10);
}
Benchmark 1: /php-dev/sapi/cli/php /mount/bc/pow/3.php
  Time (mean ± σ):     304.9 ms ±   2.9 ms    [User: 295.4 ms, System: 4.4 ms]
  Range (min … max):   299.9 ms … 308.7 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/pow/3.php
  Time (mean ± σ):     808.8 ms ±   9.0 ms    [User: 797.4 ms, System: 5.6 ms]
  Range (min … max):   799.3 ms … 827.3 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/pow/3.php' ran
    2.65 ± 0.04 times faster than '/master/sapi/cli/php /mount/bc/pow/3.php'

4:

for ($i = 0; $i < 1000; $i++) {
    bcpow('0.00000000000000123456', '2000', 10);
}
Benchmark 1: /php-dev/sapi/cli/php /mount/bc/pow/4.php
  Time (mean ± σ):     433.7 ms ±   1.9 ms    [User: 422.2 ms, System: 6.2 ms]
  Range (min … max):   431.6 ms … 437.6 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/pow/4.php
  Time (mean ± σ):      6.024 s ±  0.071 s    [User: 6.011 s, System: 0.007 s]
  Range (min … max):    5.986 s …  6.225 s    10 runs
 
  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/pow/4.php' ran
   13.89 ± 0.18 times faster than '/master/sapi/cli/php /mount/bc/pow/4.php'

@SakiTakamachi SakiTakamachi marked this pull request as ready for review March 18, 2025 00:06
@SakiTakamachi SakiTakamachi marked this pull request as draft March 18, 2025 00:31
@SakiTakamachi
Copy link
Member Author

There were still some things I wanted to change, so I reverted it to a draft.

@SakiTakamachi SakiTakamachi force-pushed the bcmath/pow branch 2 times, most recently from 33bfabf to f303bd5 Compare March 19, 2025 15:09
@SakiTakamachi SakiTakamachi marked this pull request as ready for review March 19, 2025 16:46
@SakiTakamachi
Copy link
Member Author

Ready for review.
The failed tests are irrelevant.

cc: @nielsdos

@SakiTakamachi SakiTakamachi force-pushed the bcmath/pow branch 2 times, most recently from a1f6bbf to 76d0e61 Compare March 23, 2025 10:01
@SakiTakamachi SakiTakamachi marked this pull request as draft March 23, 2025 12:04
@SakiTakamachi
Copy link
Member Author

I force pushed by mistake.

I'll open it again once I've confirmed the code 🙏

@SakiTakamachi SakiTakamachi marked this pull request as ready for review March 23, 2025 14:41
@SakiTakamachi
Copy link
Member Author

I reverted to the original code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant