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

Improve performance of the basic division algorithm. #170

Merged
merged 1 commit into from
Oct 30, 2020
Merged

Improve performance of the basic division algorithm. #170

merged 1 commit into from
Oct 30, 2020

Conversation

tczajka
Copy link
Contributor

@tczajka tczajka commented Oct 23, 2020

Avoid computing a multiple of the divisor (and thus allocating memory) in the division loop.
Instead, directly subtract a multiple of the divisor from the dividend.

Also compute a more accurate guess for the next digit of the result. The guess is now exact in
almost all cases. In those cases where it's not exact, it will be off by exactly 1.

@tczajka
Copy link
Contributor Author

tczajka commented Oct 23, 2020

Some benchmarks.

Before:

test divide_0             ... bench:         707 ns/iter (+/- 32)
test divide_1             ... bench:      18,700 ns/iter (+/- 20)
test divide_2             ... bench:     431,555 ns/iter (+/- 33,198)
test divide_big_little    ... bench:      23,667 ns/iter (+/- 23)

After:

test divide_0             ... bench:         706 ns/iter (+/- 1)
test divide_1             ... bench:      14,276 ns/iter (+/- 20)
test divide_2             ... bench:     307,589 ns/iter (+/- 569)
test divide_big_little    ... bench:      23,665 ns/iter (+/- 25)

Before:

test remainder_0          ... bench:         715 ns/iter (+/- 14)
test remainder_1          ... bench:      18,733 ns/iter (+/- 48)
test remainder_2          ... bench:     436,301 ns/iter (+/- 9,878)
test remainder_big_little ... bench:      23,477 ns/iter (+/- 32)

After:

test remainder_0          ... bench:         712 ns/iter (+/- 13)
test remainder_1          ... bench:      14,260 ns/iter (+/- 15)
test remainder_2          ... bench:     307,634 ns/iter (+/- 585)
test remainder_big_little ... bench:      23,477 ns/iter (+/- 16)

Before:

test gcd_euclid_0064 ... bench:       2,719 ns/iter (+/- 50)
test gcd_euclid_0256 ... bench:      43,623 ns/iter (+/- 149)
test gcd_euclid_1024 ... bench:     248,941 ns/iter (+/- 613)
test gcd_euclid_4096 ... bench:   1,694,518 ns/iter (+/- 2,088)

After:

test gcd_euclid_0064 ... bench:       2,757 ns/iter (+/- 34)
test gcd_euclid_0256 ... bench:      28,305 ns/iter (+/- 147)
test gcd_euclid_1024 ... bench:     159,789 ns/iter (+/- 527)
test gcd_euclid_4096 ... bench:   1,180,507 ns/iter (+/- 6,867)

Before:

test big2k_sqrt      ... bench:      32,807 ns/iter (+/- 316)
test big4k_cbrt      ... bench:      52,402 ns/iter (+/- 128)
test big4k_nth_10    ... bench:      29,072 ns/iter (+/- 343)
test big4k_nth_100   ... bench:      15,317 ns/iter (+/- 599)
test big4k_nth_1000  ... bench:      58,758 ns/iter (+/- 602)
test big4k_nth_10000 ... bench:          31 ns/iter (+/- 0)
test big4k_sqrt      ... bench:      66,525 ns/iter (+/- 588)

After:

test big2k_sqrt      ... bench:      23,926 ns/iter (+/- 169)
test big4k_cbrt      ... bench:      38,278 ns/iter (+/- 480)
test big4k_nth_10    ... bench:      22,307 ns/iter (+/- 162)
test big4k_nth_100   ... bench:      14,299 ns/iter (+/- 41)
test big4k_nth_1000  ... bench:      57,104 ns/iter (+/- 117)
test big4k_nth_10000 ... bench:          31 ns/iter (+/- 1)
test big4k_sqrt      ... bench:      47,867 ns/iter (+/- 61)

We avoid computing a multiple of the divisor (and thus allocating memory) in the division loop.
Instead, directly subtract a multiple of the divisors from the dividend.

We also compute a more accurate guess for the next digit of the result. The guess is now exact in
almost all cases. In those cases where it's not exact, it will be off by exactly 1.
@cuviper
Copy link
Member

cuviper commented Oct 30, 2020

This is great, thanks! I just pushed a rebase to resolve the merge conflicts, with no functional change.

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 30, 2020

@bors bors bot merged commit cf299cf into rust-num:master Oct 30, 2020
@tczajka tczajka deleted the div_rem_core branch October 31, 2020 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants