Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest C++ way to convert float to string

I have encountered problem of converting float to string where to_string is too slow for me as my data might involves few millions floats.

I already have solution on how to write those data out fast.

However, after solving that problem, I soon realized that the conversion of float to string is leaving a big impact.

So, is there any ideas or solution for this other than using other non standard library?

like image 701
vincent911001 Avatar asked Apr 21 '15 08:04

vincent911001


People also ask

Can we convert float to string in C?

gcvt() | Convert float value to string in C This function is used to convert a floating point number to string. Syntax : gcvt (float value, int ndigits, char * buf); float value : It is the float or double value.

Can we convert float to string?

The Float. toString() method can also be used to convert the float value to a String. The toString() is the static method of the Float class.

Does C have FTOA?

Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa().

Which C function converts a string to a float?

In the C Programming Language, the atof function converts a string to a floating-point number (double). The atof function skips all white-space characters at the beginning of the string, converts the subsequent characters as part of the number, and then stops when it encounters the first character that isn't a number.


2 Answers

Here are some of the fastest algorithms for converting floating point numbers into decimal string representation:

At the time of writing Dragonbox is the fastest of these methods, followed by Schubfach, then a variation of Grisu called Grisu-Exact (not to be confused with Grisu2 and Grisu3) and then Ryū:

enter image description here

An implementation of Dragonbox is available here. It is also included in the {fmt} library integrated into a high-level formatting API. For maximum performance you can use format_to with a stack-allocated buffer, for example:

fmt::memory_buffer buf;
fmt::format_to(buf, "{}", 4.2);
// buf.data() returns a pointer to the formatted data & buf.size() gives the size
like image 137
vitaut Avatar answered Oct 17 '22 03:10

vitaut


An optimization that comes in mind is to not directly use to_string, which creates a new string every time you call it. You probably end up copying that string too, which is not so efficient.

What you could do is to allocate a char buffer big enough to store all the string representations that you need, then use printf

http://www.cplusplus.com/reference/cstdio/printf/

reusing the same buffer all the time. If you limit the precision of your floats to a fixed amount of decimals, you can compute the offset to which your float is represented in the array.

for example if we only had an array of values:

index = 1;
float f = value[index];
//corrresponding 6 chars float
const char* s = char_array[index*1];
//the representation will start at position 6, and it will be null terminated so you can use it as a string

for clarification your char_array will look like:

1.2000\02.4324\0...
like image 1
dau_sama Avatar answered Oct 17 '22 04:10

dau_sama