Copyright(c) 2014 Milo Yip (firstname.lastname@example.org)
This benchmark evaluates the performance of conversion from double precision IEEE-754 floating point (
double) to ASCII string. The function prototype is:
void dtoa(double value, char* buffer);
The character string result must be convertible to the original value exactly via some correct implementation of
strtod(), i.e. roundtrip convertible.
dtoa() is not a standard function in C and C++.
Firstly the program verifies the correctness of implementations.
Then, one case for benchmark is carried out:
- RandomDigit: Generates 1000 random
doublevalues, filtered out
nan. Then convert them to limited precision (1 to 17 decimal digits in significand). Finally convert these numbers into ASCII.
Each digit group is run for 100 times. The minimum time duration is measured for 10 trials.
Build and Run
- Obtain premake4.
- Copy premake4 executable to
dtoa-benchmark/buildfolder (or system path).
- On Windows, build the solution at
- On other platforms, run GNU
- On success, run the
dtoaexecutable is generated at
- The results in CSV format will be written to
- Run GNU
dtoa-benchmark/resultto generate results in HTML.
The following are
sequential results measured on a PC (Core i7 920 @2.67Ghz), where
u32toa() is compiled by Visual C++ 2013 and run on Windows 64-bit. The speedup is based on
Note that the
null implementation does nothing. It measures the overheads of looping and function call.
Some results of various configurations are located at
dtoa-benchmark/result. They can be accessed online, with interactivity provided by Google Charts:
|stb_sprintf||fast sprintf replacement with
|gay||David M. Gay's
|grisu2||Florian Loitsch's Grisu2 C implementation .|
|fpconv||night-shift's Grisu2 C implementation.|
|milo||miloyip's Grisu2 C++ header-only implementation.|
tostring()is not tested as it does not fulfill the roundtrip requirement.
Grisu2 is chosen because it can generate better human-readable number and >99.9% of results are in shortest. Grisu3 needs another
dtoa()implementation for not meeting the shortest requirement.
How to add an implementation?
You may clone an existing implementation file. And then modify it. Re-run
premaketo add it to project or makefile. Note that it will automatically register to the benchmark by macro
Making pull request of new implementations is welcome.
Why not converting
It may introduce heap allocation, which is a big overhead. User can easily wrap these low-level functions to return
std::string, if needed.
dtoa()functions is needed?
They are a very common operations in writing data in text format. The standard way of
std::stringstream, often provides poor performance. The author of this benchmark would optimize the
sprintfimplementation in RapidJSON, thus he creates this project.
 Loitsch, Florian. "Printing floating-point numbers quickly and accurately with integers." ACM Sigplan Notices 45.6 (2010): 233-243.