optimized itoa function

后端 未结 8 1483
不知归路
不知归路 2021-02-04 06:48

I am thinking on how to implement the conversion of an integer (4byte, unsigned) to string with SSE instructions. The usual routine is to divide the number and store it in a loc

8条回答
  •  [愿得一人]
    2021-02-04 07:07

    This post compares several methods of integer to string conversion aka itoa. The fastest method reported there is fmt::FormatInt from the fmt library which is about 8 times faster than sprintf/std::stringstream and 5 times faster than the naive ltoa/itoa implementation (the actual numbers may of course vary depending on platform).

    Unlike most other methods fmt::FormatInt does one pass over the digits. It also minimizes the number of integer divisions using the idea from Alexandrescu's talk Three Optimization Tips for C++. The implementation is available here.

    This is of course if C++ is an option and you are not restricted by the itoa's API.

    Disclaimer: I'm the author of this method and the fmt library.

提交回复
热议问题