Daniel Lemire's blog

, 5 min read

Parsing numbers in C++: streams, strtod, from_chars

7 thoughts on “Parsing numbers in C++: streams, strtod, from_chars”

  1. degski says:

    Unfortunately my compiler does not support the from_chars function when parsing floating-point numbers.

    Don’t post this article then, or upgrade, we’re gcc-10 if I understand things correctly.

    Iff I would believe STL (lead STL dev), on Windows (the VC-STL, not some surrogate) this function (from_chars) beats anything.

    1. Don’t post this article then, or upgrade, we’re gcc-10 if I understand things correctly.

      As of November 2019, looking at the source code in the GNU GCC repo, I do not see support for floats in from_chars.

      Are you saying that it available in GNU GCC 10? When was that released?

      1. degski says:

        The world does not start and end with GNU GCC [and living on the edge 😉 is more fun].

        1. The world does not start and end with GNU GCC

          It does not look like it is available with LLVM at this time.

          If someone can help me port my code to Visual Studio, while preserving the performance counters, I will gladly run the tests.

          1. Christopher Chang says:

            You can also try the abseil implementation of from_chars (https://github.com/abseil/abseil-cpp/blob/master/absl/strings/charconv.h )

            1. Oliver Schönrock says:

              Good one! Have been looking for a solution to the apparently non-trivial problem of FAST float parsing. Clang and GCC not making any moves to support this part of C++17… 🙁

              Building abseil::string now!

            2. Oliver Schönrock says:

              bad news…

              i haven’t done very scientific testing, but it looks like absl::from_chars is dead slow for doubles

              I could do a

              std::string(const char* start_ptr, const char* end_ptr) field; // AND

              stod(field);

              and still be twice as fast as char_conv, which can do that in one step.

              (I am iterating through a 190MB mmap’ed file of floats).