|
Boost : |
From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2006-08-26 17:27:23
Hello,
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:
bool
char
wchar_t
other integral types
char*, char const*, char[], char const[]
wchar_t*, wchar_t const*, wchar_t[], wchar_t const[]
std::basic_string<>
Optimization for the following source types is OFF:
enums
float
double
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.
Diff:
cvs diff -r 1.24 -r 1.26 boost/lexical_cast.hpp
http://tinyurl.com/qu388
Performance:
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
TODO
- Optimization for float and double.
- Make MEASURE_LEXICAL_CAST_PERFORMANCE_WITHOUT_LOCALE_OVERHEAD
a public configuration parameter.
- Discuss a behavior of enums with user-defined output operator.
-- Alexander Nasonov Project Manager at Akmosoft ( http://www.akmosoft.com ) Blog: http://nasonov.blogspot.com Email: $(FirstName) dot $(LastName) at gmail dot com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk