Boost logo

Boost :

Subject: Re: [boost] lexical_cast optimization
From: remi.chateauneu_at_[hidden]
Date: 2008-12-05 22:35:19


This is about 10% faster on a Pentium:

    {
      for(const char* p = str_value; size; ++p, --size)
      {

Jonathan Brannan wrote:
> 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)
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>
>


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