Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stl vector performance

0 views
Skip to first unread message

barbaros

unread,
Feb 19, 2009, 11:44:38 AM2/19/09
to
Hello,

Is there any advantage in performing a reserve operation before
inserting a large number of zeroes in an empty vector ?

Let me be more specific. In the code below, does the second line bring
any increase in performance ?

vector<float> v;
v.reserve(1000);
v.insert(v.end(),1000,0.0);

Note: I am not interested in using directly the constructor, e.g.
vector<float> v(1000,0.0);

Thank you. Cristian Barbarosie
http://cmaf.ptmat.fc.ul.pt/~barbaros/

Victor Bazarov

unread,
Feb 19, 2009, 11:57:03 AM2/19/09
to
barbaros wrote:
> Is there any advantage in performing a reserve operation before
> inserting a large number of zeroes in an empty vector ?

Impossible to tell without measuring.

> Let me be more specific. In the code below, does the second line bring
> any increase in performance ?
>
> vector<float> v;
> v.reserve(1000);
> v.insert(v.end(),1000,0.0);

You need to have your program run in two variations, with and without
the call on the second line, and then compare the time it takes.

> Note: I am not interested in using directly the constructor, e.g.
> vector<float> v(1000,0.0);

Why? The vector is empty, per requirements. You can always construct
and new vector and assign:

v = vector<float>(1000, 0.0f);

or use the swap idiom:

vector<float>(1000, 0.0f).swap(v);

which basically does the same thing as the assignment, only [usually]
faster.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Bo Persson

unread,
Feb 19, 2009, 1:56:22 PM2/19/09
to
barbaros wrote:
> Hello,
>
> Is there any advantage in performing a reserve operation before
> inserting a large number of zeroes in an empty vector ?
>
> Let me be more specific. In the code below, does the second line
> bring any increase in performance ?
>
> vector<float> v;
> v.reserve(1000);
> v.insert(v.end(),1000,0.0);
>

Highly unlikely. The insert function sees that you want to add 1000
new members, and can do a reserve internally if needed.

Generally, you should not try to outsmart your compiler. It hardly
ever works!

Bo Persson


Juha Nieminen

unread,
Feb 20, 2009, 8:59:34 AM2/20/09
to
Bo Persson wrote:
>> vector<float> v;
>> v.reserve(1000);
>> v.insert(v.end(),1000,0.0);
>>
>
> Highly unlikely. The insert function sees that you want to add 1000
> new members, and can do a reserve internally if needed.
>
> Generally, you should not try to outsmart your compiler. It hardly
> ever works!

I think in this case it's completely the work of the library rather
than the compiler.

Juha Nieminen

unread,
Feb 20, 2009, 9:05:39 AM2/20/09
to
barbaros wrote:
> Hello,
>
> Is there any advantage in performing a reserve operation before
> inserting a large number of zeroes in an empty vector ?
>
> Let me be more specific. In the code below, does the second line bring
> any increase in performance ?
>
> vector<float> v;
> v.reserve(1000);
> v.insert(v.end(),1000,0.0);

The implementation of std::vector in your compiler would have to be
really, really stupid if its insert() function didn't allocate memory
for all the inserted elements in one single step, given that it knows
exactly how many of them are being inserted.

If you inserted using a forward iterator range, then there might be a
benefit in reserving space for the new elements (if you know how many of
them there are) because the library has no way of knowing in advance
(ie. before traversing the iterator range) how many elements will be
inserted. (If you use a random access iterator range, then some library
implementations might use template magic to detect that this is the
case, in which case it can allocate the necessary memory in advance.
However, this might not be the case with all possible STL implementations.)

barbaros

unread,
Feb 20, 2009, 4:54:39 PM2/20/09
to
Thank you all for your answers.

So, I understand the following four pieces of code should be roughly
equivalent

1: vector<float> v;
v.reserve(1000);
v.insert(v.end(),1000,0.0f);

2: vector<float> v;
v.insert(v.end(),1000,0.0f);

3: vector<float> v;


v = vector<float>(1000, 0.0f);

4: vector<float> v;
vector<float>(1000, 0.0f).swap(v);

Victor Bazarov

unread,
Feb 20, 2009, 5:19:40 PM2/20/09
to

Equivalent in the final result, yes. The #3 has a drawback of creating
duplicate storage before the temporary is destroyed, so it might be
slightly less efficient than others.

Why, do you have any timing results to share?

barbaros

unread,
Feb 21, 2009, 6:53:11 PM2/21/09
to
On 20 Fev, 22:19, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Why, do you have any timing results to share?

No, I was only trying to understand how things work in STL.

By the way, here is why I need this vector:
http://cmaf.ptmat.fc.ul.pt/~barbaros/en/tensor.cpp
It is a class for implementing a tensor of an arbitrary order.
A tensor is like a matrix, but can have more than two indices.
Vectors are order one tensors, matrices are order two tensors.

Cristian Barbarosie
http://cmaf.ptmat.fc.ul.pt/~barbaros

mojmir

unread,
Feb 24, 2009, 3:08:46 AM2/24/09
to
On Feb 22, 12:53 am, barbaros <barba...@ptmat.fc.ul.pt> wrote:

> It is a class for implementing a tensor of an arbitrary order.

in many situations you know the order ahead and therefore you
can avoid unnecessary allocations. some solution based on
parametrisation "template <size_t Order>" may perform better.
depends on our needs, of course.

best regards,
mojmir

barbaros

unread,
Feb 26, 2009, 10:31:06 AM2/26/09
to
On Feb 24, 8:08 am, mojmir <svobod...@gmail.com> wrote:
> in many situations you know the order ahead and therefore you
> can avoid unnecessary allocations. some solution based on
> parametrisation "template <size_t Order>" may perform better.
> depends on our needs, of course.

I do not know how to parametrize the number of (int) arguments of the
constructor or of the operator().

Is it possible ?

Cristian Barbarosie
http://cmaf.ptmat.fc.ul.pt/~barbaros

Victor Bazarov

unread,
Feb 26, 2009, 12:17:51 PM2/26/09
to
barbaros wrote:
> On Feb 24, 8:08 am, mojmir <svobod...@gmail.com> wrote:
>> in many situations you know the order ahead and therefore you
>> can avoid unnecessary allocations. some solution based on
>> parametrisation "template <size_t Order>" may perform better.
>> depends on our needs, of course.
>
> I do not know how to parametrize the number of (int) arguments of the
> constructor or of the operator().
>
> Is it possible ?

Not currently, AFAIK.

James Kanze

unread,
Mar 1, 2009, 7:34:06 PM3/1/09
to

And the complexity requirements of the function in the standard
require the implementation of the library to do the right thing.

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

0 new messages