incubator-stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Brown" <mark.g.br...@gmail.com>
Subject stdcxx stringstreams 2x slower than gcc
Date Tue, 06 May 2008 22:57:23 GMT
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);
}

Mime
View raw message