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

Reduce the computational complexity of formatting #216

Merged
merged 1 commit into from Aug 28, 2021

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Aug 27, 2021

This only matters for very large values, more than about 1000 digits, but it does make a big difference for those.

For the included benchmarks, I got these base-10 results before:

test to_str_radix_10      ... bench:       2,095 ns/iter (+/- 20)
test to_str_radix_10_2    ... bench:     150,702 ns/iter (+/- 1,231)

The _2 variant is over 10,000 bits. With the new code, I get these results:

test to_str_radix_10      ... bench:       2,065 ns/iter (+/- 22)
test to_str_radix_10_2    ... bench:      44,130 ns/iter (+/- 231)

@cuviper
Copy link
Member Author

cuviper commented Aug 27, 2021

Less rigorously, I swept over a series of large values to help determine the threshold for enabling this new code.

Before:

2^2^10-1 has 309 digits, 3.14µs elapsed
2^2^11-1 has 617 digits, 7.14µs elapsed
2^2^12-1 has 1234 digits, 26.64µs elapsed
2^2^13-1 has 2467 digits, 99.961µs elapsed
2^2^14-1 has 4933 digits, 390.405µs elapsed
2^2^15-1 has 9865 digits, 1.560669ms elapsed
2^2^16-1 has 19729 digits, 6.168387ms elapsed
2^2^17-1 has 39457 digits, 24.690763ms elapsed
2^2^18-1 has 78914 digits, 98.170569ms elapsed
2^2^19-1 has 157827 digits, 395.343446ms elapsed
2^2^20-1 has 315653 digits, 1.572607067s elapsed

After:

2^2^10-1 has 309 digits, 2.98µs elapsed
2^2^11-1 has 617 digits, 7.03µs elapsed
2^2^12-1 has 1234 digits, 16.53µs elapsed
2^2^13-1 has 2467 digits, 39.081µs elapsed
2^2^14-1 has 4933 digits, 105.181µs elapsed
2^2^15-1 has 9865 digits, 323.034µs elapsed
2^2^16-1 has 19729 digits, 1.122664ms elapsed
2^2^17-1 has 39457 digits, 3.710655ms elapsed
2^2^18-1 has 78914 digits, 13.514685ms elapsed
2^2^19-1 has 157827 digits, 48.508852ms elapsed
2^2^20-1 has 315653 digits, 187.755271ms elapsed

I honestly expect there's still room for improvement, but this is so much better that I think it's worth it already.

@cuviper
Copy link
Member Author

cuviper commented Aug 28, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Aug 28, 2021

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.

None yet

1 participant