atom feed5 messages in org.apache.incubator.stdcxx-devRe: stdcxx stringstreams 2x slower th...
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 
Subject:Re: stdcxx stringstreams 2x slower than gcc
From:Martin Sebor (seb@roguewave.com)
Date:May 6, 2008 8:42:32 pm
List:org.apache.incubator.stdcxx-dev

Mark Brown wrote:

Hi again!

Hey Mark!

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?

Funny! I get the opposite result with gcc 4.3 in an 8D (shared, optimized, NOT reentrant) build. Your program runs in 9 seconds with stdcxx and over 15 seconds with g++. In 12D though, and with gcc 4.1.2, the results are reversed. In 12D with gcc 4.2, both programs take 15 seconds.

I think the difference between 8D and 12D is due to the mutex in stingstream. Even though there's no locking the mutex still is initialized and that's what I suspect accounts for the slow runtimes. We need an issue to remind us to fix it as soon as binary compatibility permits it. As for gcc, I can only guess that libstdc++ has regressed between 4.1.2 and 4.3.

Martin

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