Boost logo

Boost :

From: Stewart, Robert (stewart_at_[hidden])
Date: 2002-03-05 14:44:24


From: rwgk [mailto:rwgk_at_[hidden]]
>
> --- In boost_at_y..., Damien Fisher <damien_at_m...> wrote:
> > > Why not? If you write vector<complex<double> > vec(10);
> you have made
> > > a conscious decision to create a vector that started with 10
> complex
> > > numbers. Its not unreasonable for a user to use one of those
> complex
> > > numbers. If you just wanted to reserve space for 10 complex
> numbers you
> > > could have just used the default constructor then called reserve
> ().
> > > Besides, the default constructor is inline, so how slow could it
> be?
> > >
> > 100 x 100 matrix of complex numbers => 100 * 100 * 2 unnecessary
> > initializations. and that's not even a particularly large example.
>
> Unfortunately the reserve() solution eventually requires
> use of push_back(), which is also slow.

If you use push_back() following reserve(), there is no performance penalty.
The push_back()'ed object is copied into the next element. If you exceed
the size allocated with reserve(), then push_back() will cause reallocation
and copying, so be sure to choose the right size when calling reserve().

Rob
Susquehanna International Group, LLP
http://www.sig.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk