Hi again!
While testing my own implementation of lexical cast I discovered that
the stdcxx stringstreams are nearly twice as slow as gcc's. I created
a small test program to convince myself of the difference. On my
x86_64 Linux PC it takes 16 seconds to run through 10 million loops
when using stdcxx but just 9 seconds with gcc. Is there some option or
trick that I don't know about to speed things up? Or maybe a quick fix
that I could work on?
Cheers
-- Mark
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <typeinfo>
template <class To, class From>
To lex_cast(const From &source)
{
std::stringstream converter;
To dest;
if ((converter << source) && (converter >> dest))
return dest;
throw std::bad_cast();
}
int main(int argc, char *argv[])
{
long loops = atol(argv [1]);
long long sum = 0;
std::string str;
for (long i = 0; i < loops; ++i) {
str = lex_cast<std::string>(i);
sum += str.length();
}
printf("%llu\n", sum);
}