5 messages in org.apache.incubator.stdcxx-devstdcxx stringstreams 2x slower than gcc
FromSent OnAttachments
Mark BrownMay 6, 2008 3:57 pm 
Martin SeborMay 6, 2008 8:42 pm 
Eric LemingsMay 7, 2008 7:30 am 
Martin SeborMay 7, 2008 7:38 am 
Eric LemingsMay 7, 2008 8:01 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:stdcxx stringstreams 2x slower than gccActions
From:Mark Brown (mark@gmail.com)
Date:May 6, 2008 3:57:00 pm
List:org.apache.incubator.stdcxx-dev

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); }