Double to String Conversion
Converting numbers to string is a frequent operation when we have to transmit data as a character string to any system.
In ultra low latency systems this conversion can contribute to almost 20% of the system latency, when the precision
requirement is higher.
Let us consider the popular ways of converting double to string in C++.
stringstream
std::stringstream provides a simple and easy way of converting double to string.Here is a code snippet.
std::stringstream strval; double dval = 1729.314; strval<<dval; const char* finalValue = strval.str().c_str();
to_string()
C++11 strings library provides a convenient method to_string() to convert numbers to string.double dval = 1729.314; const char* finalValue = std::to_string(dval).to_string()
sprintf()
This is the C way of converting double to string.char finalValue[1024]; double dval = 1729.314; sprintf(finalValue, "%f", dval);
Which one do you prefer in a latency sensitive project?
Let us analyze the performance of each of the above methods.
But buffer we proceed, let us think of a custom way of doing it on our own for better performance.
Custom Implementation
Integer to string conversion can be cached if we know the range of integers we are going to use. For example, we can cache a millon integers which could cost hardly 10 MB.We can cut double precision numbers to two integers, numbers before and after the decimal separator. Then its a matter of converting these integers and concatenating both with decimal separator.
The only edge case we need to handle here is when the number after decimal separator has leading 0s, like 1.02.
One idea is to multiply the number after decimal separator with power of 10 (better to take 10^maximum precision). If the number of digits is less than the power of 10(maximum precision), then we have to prepend the number with 0s.
Lets now see the performance comparison of all the above approaches:
The above benchmark is done by executing the same example using all the 4 approaches discussed before.
Benchmark is done on converting random double precision numbers of 4 digits decimal precision, 1000 times.
From the above graph you can see that stringstream is the worst.
Median latency for stringstream is ~5.8 micro seconds.
stod() is the next better, which is ~4.2 micro seconds.
sprintf() provides event better, which is ~3 micro seconds. So the powerful C function beats its C++ counter parts.
Finally our custom implementation outperforms all the standard functions by miles. Its median latency is ~500 nano seconds which is more than 6 times better than sprintf!!
Conclusion
For ultra low latency applications its better to have handcrafted functions for double conversion.Standard library functions is not fast enough especially if the decimal precision required is more.
Custom implementation can gain lot of advantage depending on the use case. For eg: in our case we have used more memory for gaining on reducing latency. Its a better bet if you have enough RAM and has more stricter latency requirements.
No comments :
Post a Comment