Skip to content

ext/bcmath: optimized divmod() and mod() take 2 #18058

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

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

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Mar 14, 2025

Benchmarks

1:

for ($i = 0; $i < 2000000; $i++) {
    bcdiv('1.23', '2', 5);
    // bcdivmod('1.23', '2', 5);
    // bcmod('1.23', '2', 5);
}

2:

for ($i = 0; $i < 2000000; $i++) {
    bcdiv('5.2345678', '2.1234567', 10);
    // bcdivmod('5.2345678', '2.1234567', 10);
    // bcmod('5.2345678', '2.1234567', 10);
}

3:

for ($i = 0; $i < 2000000; $i++) {
    bcdiv('12345678.901234', '1.2345678912', 10);
    // bcdivmod('12345678.901234', '1.2345678912', 10);
    // bcmod('12345678.901234', '1.2345678912', 10);
}

div

In theory, it will be slower because there are more branches, but it seems to make little difference.

1:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/1.php
  Time (mean ± σ):     308.9 ms ±   6.5 ms    [User: 297.1 ms, System: 6.0 ms]
  Range (min … max):   300.9 ms … 323.5 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/1.php
  Time (mean ± σ):     303.5 ms ±   5.3 ms    [User: 292.3 ms, System: 5.5 ms]
  Range (min … max):   298.1 ms … 313.0 ms    10 runs
 
Summary
  '/master/sapi/cli/php /mount/bc/divmod/1.php' ran
    1.02 ± 0.03 times faster than '/php-dev/sapi/cli/php /mount/bc/divmod/1.php'

2:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/2.php
  Time (mean ± σ):     379.5 ms ±   6.8 ms    [User: 370.4 ms, System: 3.6 ms]
  Range (min … max):   368.5 ms … 390.2 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/2.php
  Time (mean ± σ):     386.2 ms ±  18.8 ms    [User: 376.2 ms, System: 4.6 ms]
  Range (min … max):   370.3 ms … 432.2 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/2.php' ran
    1.02 ± 0.05 times faster than '/master/sapi/cli/php /mount/bc/divmod/2.php'

3:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/3.php
  Time (mean ± σ):     526.3 ms ±  14.4 ms    [User: 514.7 ms, System: 6.2 ms]
  Range (min … max):   507.4 ms … 557.3 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/3.php
  Time (mean ± σ):     529.8 ms ±  14.2 ms    [User: 519.3 ms, System: 4.9 ms]
  Range (min … max):   517.2 ms … 553.8 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/3.php' ran
    1.01 ± 0.04 times faster than '/master/sapi/cli/php /mount/bc/divmod/3.php'

divmod

This is faster because there are no extra calculations involved.

1:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/1.php
  Time (mean ± σ):     457.5 ms ±   9.5 ms    [User: 446.7 ms, System: 5.2 ms]
  Range (min … max):   448.2 ms … 473.7 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/1.php
  Time (mean ± σ):     518.5 ms ±   8.9 ms    [User: 508.0 ms, System: 5.3 ms]
  Range (min … max):   511.2 ms … 535.5 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/1.php' ran
    1.13 ± 0.03 times faster than '/master/sapi/cli/php /mount/bc/divmod/1.php'

2:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/2.php
  Time (mean ± σ):     487.0 ms ±   9.8 ms    [User: 476.0 ms, System: 4.4 ms]
  Range (min … max):   476.4 ms … 503.4 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/2.php
  Time (mean ± σ):     585.8 ms ±   8.8 ms    [User: 576.1 ms, System: 4.2 ms]
  Range (min … max):   576.3 ms … 607.8 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/2.php' ran
    1.20 ± 0.03 times faster than '/master/sapi/cli/php /mount/bc/divmod/2.php'

3:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/3.php
  Time (mean ± σ):     581.4 ms ±   4.6 ms    [User: 570.5 ms, System: 5.1 ms]
  Range (min … max):   573.4 ms … 590.0 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/3.php
  Time (mean ± σ):     791.4 ms ±  26.3 ms    [User: 780.6 ms, System: 5.2 ms]
  Range (min … max):   771.4 ms … 843.2 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/3.php' ran
    1.36 ± 0.05 times faster than '/master/sapi/cli/php /mount/bc/divmod/3.php'

mod

It's faster for the same reasons as divmod, and it's even faster because it doesn't need to generate bc_num for quot.

1:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/1.php
  Time (mean ± σ):     321.5 ms ±  10.3 ms    [User: 311.8 ms, System: 4.4 ms]
  Range (min … max):   311.4 ms … 348.2 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/1.php
  Time (mean ± σ):     401.1 ms ±  10.4 ms    [User: 391.1 ms, System: 4.8 ms]
  Range (min … max):   389.9 ms … 426.9 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/1.php' ran
    1.25 ± 0.05 times faster than '/master/sapi/cli/php /mount/bc/divmod/1.php'

2:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/2.php
  Time (mean ± σ):     348.5 ms ±   6.4 ms    [User: 338.2 ms, System: 4.7 ms]
  Range (min … max):   337.8 ms … 358.3 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/2.php
  Time (mean ± σ):     481.3 ms ±  19.2 ms    [User: 470.4 ms, System: 5.4 ms]
  Range (min … max):   468.1 ms … 528.5 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/2.php' ran
    1.38 ± 0.06 times faster than '/master/sapi/cli/php /mount/bc/divmod/2.php'

3:

Benchmark 1: /php-dev/sapi/cli/php /mount/bc/divmod/3.php
  Time (mean ± σ):     433.1 ms ±   9.3 ms    [User: 422.9 ms, System: 4.7 ms]
  Range (min … max):   424.0 ms … 456.8 ms    10 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/divmod/3.php
  Time (mean ± σ):     660.2 ms ±   7.5 ms    [User: 650.1 ms, System: 4.7 ms]
  Range (min … max):   650.9 ms … 673.1 ms    10 runs
 
Summary
  '/php-dev/sapi/cli/php /mount/bc/divmod/3.php' ran
    1.52 ± 0.04 times faster than '/master/sapi/cli/php /mount/bc/divmod/3.php'

@SakiTakamachi SakiTakamachi force-pushed the bcmath/mod_divmod branch 2 times, most recently from 430fb99 to e91f32e Compare April 26, 2025 03:46
@SakiTakamachi SakiTakamachi force-pushed the bcmath/mod_divmod branch 2 times, most recently from e29f0f3 to f0e9762 Compare April 26, 2025 05:19
@SakiTakamachi SakiTakamachi marked this pull request as ready for review April 26, 2025 06:27
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