Re: + or += (was: why not use i++ instead of ++i in for()?)
From: Bo Persson (bop_at_gmb.dk)
Date: 02/24/04
- Previous message: Martin Sebor: "Re: Limitations"
- In reply to: Hendrik Schober: "+ or += (was: why not use i++ instead of ++i in for()?)"
- Next in thread: Hendrik Schober: "Re: + or += (was: why not use i++ instead of ++i in for()?)"
- Reply: Hendrik Schober: "Re: + or += (was: why not use i++ instead of ++i in for()?)"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 24 Feb 2004 19:40:57 +0100
"Hendrik Schober" <SpamTrap@gmx.de> skrev i meddelandet
news:usBYr7v%23DHA.684@tk2msftngp13.phx.gbl...
> Bo Persson <bop@gmb.dk> wrote:
> > [...]
> > > > > s = a + b + c + d;
> > > > >
> > > > > is not as efficient as
> > > >
> > > > This all depends on the implementation, and the lengths of the
strings
> > > > involved. If some, or all, of the strings are short, there might not
be many
> > > > allocations at all.
> > >
> > > Still, strings are copied several times.
> >
> > Sure, but if the strings are short, and the implementation uses the
small
> > string optimization, there doesn't have to be any dynamic memory
allocations
> > at all.
>
>
> Yes, but the same is always true for
> 'operator+=()'. So this argument is
> moot.
But I'm not the one that said that the following ugly-looking code is always
better.
>
> > > > > s = a;
> > > > > s += b;
> > > > > s += c;
> > > > > s += d;
> > > > >
>
> > > Am I missing something?
> >
> > That the OP made a statement that some obscure code is always more
efficient
> > than the simple and obvious version.
>
> I don't think the code is obscure, but YMMV.
Obviously. :-)
> I
> don't think it will always be more efficient
> either, but I do think it has a good chance
> to be in many cases. OTOH, I still fail to
> see how it could ever be less efficient.
> I do use such code in libraries that might
> be used where performance matters. I would
> be very interested in an example where this
> might do harm.
So you say that it might be faster, sometimes. The OP said it was more
efficient, always.
>
> > It's not, it all depends on the
dynamic
> > sizes of the strings involved.
>
> With the small string optimization (SSO),
> allocations might be prevented depending on
> the size of the strings. OTOH, copying cannot
> be prevented by SSO.
> FWIW, a somehow canonical implementation of
> 'operator+()' looks like this:
>
> T operator+(const T& l, const T& r)
> {
> T tmp(l);
> tmp += r; // invoking 'operator+=()' here
> return tmp;
> }
>
> This makes it obvious that, regarding
> performance, 'operator+()' can never be
> better than 'operator+=()', but it will
> often be worse.
>
If you are unlucky, this will allocate a buffer and copy l once, then
reallocate the buffer and copy l again, before adding r.
Another implementation looks like this:
T operator+(const T& l, const T& r)
{
T tmp;
tmp.reserve(l.size() + r.size());
return tmp.append(l).append(r);
}
A maximum of one memory allocation. Copies each character exactly once.
> > Try this for example:
> >
> > #include <iostream>
> > #include <string>
> >
> > int main()
> > {
> > const std::string a = "abcd";
> > const std::string b = "bcda";
> > const std::string c = "cdab";
> > const std::string d = "dabc";
> >
> > std::string s = a + b + c + d;
> >
> > std::cout << s;
> >
> > return 0;
> > }
> >
> > On my system there are no dynamic allocations for the strings. It's all
done
> > on the local stack.
>
>
> Still, this creates three temporaries,
> where Mark's code creates none. And his
> code will not trigger any allocations
> either.
> What is your point?
My point is:
Best case, three temporaries on stack, but no allocations.
Mark's best case, no temporaries, but s is updated 3 more times.
Worst case, three temporaries allocated, s is reallocated on assignment.
Four in total.
Mark's worst case, s is reallocated on assignment, and for each append. Four
in total.
s = a + b + c + d;
Looks much better. :-)
Bo Persson
- Previous message: Martin Sebor: "Re: Limitations"
- In reply to: Hendrik Schober: "+ or += (was: why not use i++ instead of ++i in for()?)"
- Next in thread: Hendrik Schober: "Re: + or += (was: why not use i++ instead of ++i in for()?)"
- Reply: Hendrik Schober: "Re: + or += (was: why not use i++ instead of ++i in for()?)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|