My laptop which I'll be running the tests on is a Mobile Intel Pentium 4M 2200 MHz with 768 MB DDR SDRAM. I'll be compiling with Microsoft Visual C++ 7.1 in release mode. The tests are simple iteration tests running the same code over and over again a 1000 times. I know that this is not representative of real performance do to caching affects, but even this simple testing can be eye opening.
Today lets look at boost::lexical_cast
std::string twentythree = boost::lexical_cast
If I do this on my laptop it runs in about 7 microseconds. Not too bad. However, if I use a C approach and do the following:
ACE_OS::sprintf(buf, "%d", 23);
It runs in 0.3 microseconds, which, I think you would agree, is better.
The lexical_cast is safer and more general but is it worth the difference? I would say it depends.
So why is lexical_cast so much slower? Well, its because it uses std::stringstream. Take a look at the following:
ss << 23;
The above takes 6.7 microseconds, which is clearly most of the effort of lexical_cast.