You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Mark Brown <ma...@gmail.com> on 2008/05/07 00:57:23 UTC

stdcxx stringstreams 2x slower than gcc

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

RE: stdcxx stringstreams 2x slower than gcc

Posted by Eric Lemings <Er...@roguewave.com>.
 

> -----Original Message-----
> From: Martin Sebor [mailto:msebor@gmail.com] On Behalf Of Martin Sebor
> Sent: Wednesday, May 07, 2008 8:39 AM
> To: dev@stdcxx.apache.org
> Subject: Re: stdcxx stringstreams 2x slower than gcc
> 
> Eric Lemings wrote:
> >  
> > 
> >> -----Original Message-----
> >> From: Martin Sebor [mailto:msebor@gmail.com] On Behalf Of 
> Martin Sebor
> >> Sent: Tuesday, May 06, 2008 9:43 PM
> >> To: dev@stdcxx.apache.org
> >> Subject: Re: stdcxx stringstreams 2x slower than gcc
> >>
> > ...
> >> 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.
> > 
> > I conclude from this that the mutex is exposed in the public ABI?
> > If so, why?  I would think the mutex would not need be a part of
> > the public ABI.
> 
> You're right, it shouldn't be but, regretfully, it is. Hence
> STDCXX-914: http://issues.apache.org/jira/browse/STDCXX-914
> (With C++ templates it can be a challenge to avoid exposing
> implementation details like this)

Challenging but not impossible?  ;)

Brad.

Re: stdcxx stringstreams 2x slower than gcc

Posted by Martin Sebor <se...@roguewave.com>.
Eric Lemings wrote:
>  
> 
>> -----Original Message-----
>> From: Martin Sebor [mailto:msebor@gmail.com] On Behalf Of Martin Sebor
>> Sent: Tuesday, May 06, 2008 9:43 PM
>> To: dev@stdcxx.apache.org
>> Subject: Re: stdcxx stringstreams 2x slower than gcc
>>
> ...
>> 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.
> 
> I conclude from this that the mutex is exposed in the public ABI?
> If so, why?  I would think the mutex would not need be a part of
> the public ABI.

You're right, it shouldn't be but, regretfully, it is. Hence
STDCXX-914: http://issues.apache.org/jira/browse/STDCXX-914
(With C++ templates it can be a challenge to avoid exposing
implementation details like this)

Martin


RE: stdcxx stringstreams 2x slower than gcc

Posted by Eric Lemings <Er...@roguewave.com>.
 

> -----Original Message-----
> From: Martin Sebor [mailto:msebor@gmail.com] On Behalf Of Martin Sebor
> Sent: Tuesday, May 06, 2008 9:43 PM
> To: dev@stdcxx.apache.org
> Subject: Re: stdcxx stringstreams 2x slower than gcc
> 
...
> 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.

I conclude from this that the mutex is exposed in the public ABI?
If so, why?  I would think the mutex would not need be a part of
the public ABI.

Brad.

Re: stdcxx stringstreams 2x slower than gcc

Posted by Martin Sebor <se...@roguewave.com>.
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);
> }