Boost logo

Boost :

Subject: Re: [boost] lexical_cast optimization
From: Jonathan Brannan (kickace_at_[hidden])
Date: 2008-12-05 10:40:13


we have found that atoi/strtol/strtoll/strtod are slow compared to the
optimized output (gcc -O2/3) of a very simple * 10 loop. The C number
converters work for a large varity of bases, which makes them slower
than needed for base 10.

we use something like:

  template<typename TInteger>
    bool validated_to_unsigned_int(
        const char* str_value, size_t size,
        TInteger& result)
    {
      size_t i = 0;
      for(const char* p = str_value; i < size; ++p, ++i)
      {
        char digit = *p;
        if (digit >= '0' && digit <='9')
        {
          result = result * 10 + digit - '0';
        }
        else
        {
          return false;
        }
      }
      return true;
    }

with wrappers for signed and ones that throw exceptions. We have a
few converters that are faster, but only without error checking (base
16 conversion with lookup table works for small numbers, for example)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk