ulfjack / ryu

Converts floating point numbers to decimal strings
Apache License 2.0
1.2k stars 100 forks source link

Reimplemented to_chars of d2s.c #195

Closed cassioneri closed 1 year ago

cassioneri commented 3 years ago

Local tests pass and I get a performance boost of ~6% for double to string conversion.

It's difficult to pinpoint exactly what makes the performance better. Here are some clues. First I've removed the old loop (basically, I've unrolled it)

while (output2 >= 10000) {

This suggests (very broadly speaking) a complexity O(N) (where N is the input's number of digits). My code, instead, in each "iteration" compares what used to be output2 against 10⁸, 10⁴, 10² and 10¹ which indicates a complexity O(log(N)). Also, I don't calculate the number of digits beforehand. (See the comment about decimalLength17 is no longer called.)

Also, I've reduced the number of pointer/array arithmetic operations and the writing is done in a single direction from smaller to larger addresses.

If all that is fine, I guess a similar work could be done for the float to string conversion.