Skip to content

[WIP] ext/bcmath: optimized bcsqrt() #18771

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

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

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Jun 5, 2025

Benchmark

The smaller the number of digits handled, the greater the performance improvement.

Small size value (fast path)

Code:

for ($i = 0; $i < 1000000; $i++) {
    bcsqrt(20, 6);
}

Result:

Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/sqrt/0.php
  Time (mean ± σ):      87.5 ms ±   1.0 ms    [User: 81.6 ms, System: 3.5 ms]
  Range (min … max):    86.4 ms …  91.5 ms    33 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/sqrt/0.php
  Time (mean ± σ):     748.1 ms ±   6.3 ms    [User: 741.6 ms, System: 3.9 ms]
  Range (min … max):   741.1 ms … 758.4 ms    10 runs
 
Summary
  '/php-dev2/sapi/cli/php /mount/bc/sqrt/0.php' ran
    8.55 ± 0.12 times faster than '/master/sapi/cli/php /mount/bc/sqrt/0.php'

Middle size value (standard path)

Code:

for ($i = 0; $i < 500000; $i++) {
    bcsqrt('15151324141414.412312232141241', 0);
}

Result:

Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/sqrt/1.php
  Time (mean ± σ):     136.2 ms ±   0.5 ms    [User: 129.9 ms, System: 3.9 ms]
  Range (min … max):   135.5 ms … 137.2 ms    22 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/sqrt/1.php
  Time (mean ± σ):     795.9 ms ±  29.1 ms    [User: 789.5 ms, System: 4.0 ms]
  Range (min … max):   782.8 ms … 878.1 ms    10 runs

Summary
  '/php-dev2/sapi/cli/php /mount/bc/sqrt/1.php' ran
    5.85 ± 0.21 times faster than '/master/sapi/cli/php /mount/bc/sqrt/1.php'

Big size value (standard path)

Code:

for ($i = 0; $i < 100; $i++) {
    bcsqrt('15151324141414.412312232141241', 3000);
}

Result:

Benchmark 1: /php-dev2/sapi/cli/php /mount/bc/sqrt/2.php
  Time (mean ± σ):     250.2 ms ±   1.4 ms    [User: 244.2 ms, System: 3.5 ms]
  Range (min … max):   248.5 ms … 253.7 ms    12 runs
 
Benchmark 2: /master/sapi/cli/php /mount/bc/sqrt/2.php
  Time (mean ± σ):     276.8 ms ±   1.0 ms    [User: 270.8 ms, System: 3.5 ms]
  Range (min … max):   274.9 ms … 278.5 ms    10 runs
 
Summary
  '/php-dev2/sapi/cli/php /mount/bc/sqrt/2.php' ran
    1.11 ± 0.01 times faster than '/master/sapi/cli/php /mount/bc/sqrt/2.php'

@SakiTakamachi SakiTakamachi force-pushed the bcmath/optimize_sqrt branch 7 times, most recently from bbfe6d5 to 9cf6b03 Compare June 6, 2025 13:05
@SakiTakamachi SakiTakamachi changed the title [WIP] ext/bcmath: optimized sqrt() [WIP] ext/bcmath: optimized bcsqrt() Jun 7, 2025
@SakiTakamachi SakiTakamachi force-pushed the bcmath/optimize_sqrt branch 17 times, most recently from 77116c3 to 45ce75d Compare June 8, 2025 13:08
@SakiTakamachi SakiTakamachi force-pushed the bcmath/optimize_sqrt branch 2 times, most recently from 5e82e80 to b4071ec Compare June 8, 2025 17:28
@SakiTakamachi SakiTakamachi force-pushed the bcmath/optimize_sqrt branch 5 times, most recently from 9356bd9 to de00485 Compare June 9, 2025 03:11
@SakiTakamachi SakiTakamachi force-pushed the bcmath/optimize_sqrt branch 2 times, most recently from 0bf3d72 to 90694a0 Compare June 9, 2025 05:03
@SakiTakamachi SakiTakamachi force-pushed the bcmath/optimize_sqrt branch from 90694a0 to d025051 Compare June 9, 2025 07:09
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