https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71108

            Bug ID: 71108
           Summary: to_string is relatively slow
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adrian.wielgosik at gmail dot com
  Target Milestone: ---

Currently to_string is implemented in terms of vsnprintf, which makes it close
in performance to the sprintf family. Meanwhile hand-written implementations of
to_string can be up to an order of magnitude faster.

For example, this function is 3-7x faster than 6.1 std::to_string(unsigned int)
in a microbenchmark.

std::string to_string(unsigned int value)
{
    char buffer[20];
    char *end = std::end(buffer);
    char *it = end;

    do {
        it--;
        *it = '0' + value % 10;
        value /= 10;
    } while (value);

    return std::string(it, end);
}

Reply via email to