dtolnay / itoa

Fast function for printing integer primitives to a decimal string
Apache License 2.0
306 stars 37 forks source link

Optimize udivmod_1e19 function #27

Closed Kogia-sima closed 3 years ago

Kogia-sima commented 3 years ago

This PR optimize udivmod_1e19 function based on the algorithm provided in [1]. This optimization dramatically improve performance of formatting u128 type.

Performance

previous results

test bench_itoa_fmt::bench_u128_0     ... bench:          19 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_max   ... bench:         423 ns/iter (+/- 3)
test bench_itoa_write::bench_u128_0   ... bench:          17 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_max ... bench:         422 ns/iter (+/- 13)
test bench_std_fmt::bench_u128_0      ... bench:          78 ns/iter (+/- 2)
test bench_std_fmt::bench_u128_max    ... bench:         514 ns/iter (+/- 9)

new results

test bench_itoa_fmt::bench_u128_0     ... bench:          19 ns/iter (+/- 3)
test bench_itoa_fmt::bench_u128_max   ... bench:          75 ns/iter (+/- 3)
test bench_itoa_write::bench_u128_0   ... bench:          17 ns/iter (+/- 1)
test bench_itoa_write::bench_u128_max ... bench:          75 ns/iter (+/- 3)
test bench_std_fmt::bench_u128_0      ... bench:          74 ns/iter (+/- 3)
test bench_std_fmt::bench_u128_max    ... bench:         512 ns/iter (+/- 19)

Reference

[1] T. Granlund and P. Montgomery, “Division by Invariant Integers Using Multiplication” in Proc. of the SIGPLAN94 Conference on Programming Language Design and Implementation, 1994, pp. 61–72

dtolnay commented 3 years ago

Published in itoa 0.4.8.