A double-to-string conversion algorithm based on Schubfach
- Round trip guarantee
- Shortest decimal representation
- Correct rounding
- High performance
- Fast compile time
- Zero dependencies
- Small, clean codebase consisting of one source file and one header
- Permissive license
#include "zmij.h"
#include <stdio.h>
int main() {
char buf[zmij::buffer_size];
zmij::dtoa(6.62607015e-34, buf);
puts(buf);
}More than 3x faster than Ryu used by multiple C++ standard library implementations and ~2x faster than Schubfach on dtoa-benchmark run on Apple M1.
| Function | Time (ns) | Speedup |
|---|---|---|
| ostringstream | 902.796 | 1.00x |
| sprintf | 737.769 | 1.22x |
| doubleconv | 85.790 | 10.52x |
| to_chars | 42.779 | 21.10x |
| ryu | 38.631 | 23.37x |
| schubfach | 25.292 | 35.69x |
| fmt | 22.384 | 40.33x |
| dragonbox | 21.162 | 42.66x |
| zmij | 12.360 | 73.04x |
| null | 0.957 | 943.30x |
Compile time is ~60ms by default and ~68ms with optimizations enabled as measured by
% time c++ -c -std=c++20 schubfach.cc [-O2]
taking the best of 3 runs.
- Selection from 1-3 candidates instead of 2-4
- Fewer integer multiplications in the shorter case
- Faster logarithm approximations
- Faster division and modulo
- Fewer conditional branches
- More efficient significand and exponent output