Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2006-08-26 17:27:23

I recently commited the version 1.26 of lexical_cast.hpp
that optimizes many combinations of types.

How it works:

The lexical_cast reserves a local buffer for bool, char, wchar_t
and other integral types. Then it puts a string representation of
a source to the buffer. Currently, all such algorithms don't use
ostream at all but it's easy to implement a generic algorithm
that sets a put area of a ostream object to point to that buffer
and puts a string representation to the buffer.
When a source type is a pointer to char/wchar_t array or
basic_string<>, its value is already a string representation of
itself and a call to ostream output operator is omitted.

Futher optimization is applied when a target type is either
char/wchar_t or basic_string<>. In this case, std streams are
not involved in a conversion at all.
For all other target types, basic_istream object is contructed,
its get area is set to a string representation of a source type
and the operator>> is called.

Optimization for the following source types is ON:
    other integral types
    char*, char const*, char[], char const[]
    wchar_t*, wchar_t const*, wchar_t[], wchar_t const[]

Optimization for the following source types is OFF:
    signed char
    unsigned char
    signed char*, signed char const*, signed char[], signed char const[]
    unsigned char*, unsigned char const*, unsigned char[], unsigned char const[]
    all other types not listed in ON list

Tested on VC6, VC7.1, VC8, gcc 3.4 and Borland 5.5. I couldn't run
the test on Borland so I tested only that it compiles.

cvs diff -r 1.24 -r 1.26 boost/lexical_cast.hpp

Compiled with gcc 3.4.4 on FreeBSD 6.1 with -O2 flag turned on.

A coversion from an int value [0, 9] to char.
With Boost 1.33.1 - 2.012 s
With Boost HEAD - 0.322 s, 6.24 times faster
Ignore locales - 0.083 s

A coversion from an int value [0, 999999] to std::string.
With Boost 1.33.1 - 2.516 s
With Boost HEAD - 0.844 s, 2.98 times faster
Ignore locales - 0.626 s


- Optimization for float and double.
  a public configuration parameter.
- Discuss a behavior of enums with user-defined output operator.

Alexander Nasonov
Project Manager at Akmosoft ( )
Email: $(FirstName) dot $(LastName) at gmail dot com

Boost list run by bdawes at, gregod at, cpdaniel at, john at