开发者

Before or after when adding to sets in C++

Given

my_type m;
std::vector<my_type> v;
开发者_开发百科

Which runs more quickly?

m.generate_data_inside_self();
v.push_back(m);

Or

v.push_back(m);
v[0].generate_data_inside_self();

If the vector held pointers to the my_types then both would seem about the same.

However when copying in the whole my_type object as in this example I think the 2nd would be faster as there is less to copy as the extra data only comes into existance after "m" is inside "v".

edit:

In the example in my program my_type looks sort of like this.

my_type
{
    private:
        std::vector<unsigned short> data; //empty after construction

    public:
        //no destructors, assignment operators
        //copy constructors etc... explicitly (are) defined
        generate_data_inside_self() //populates data
        {
            //contains for example a loop that populates
            //"data" with some (lets say 50) values
        }
}


Add it when the complexity of copy constructor/operator == is smaller. If you are generating data, most likely increasing that complexity, insert before generating.

If you have many vector copies and you are concerned about performance, my suggestion is to have a vector of pointers and new (and of course one day delete) the objects and put them in the vector. That way, the cost of inserting in vector is not dependent on the complexity of the object.


Sorry, but it depends too much on what your type is. If it holds pointers to some big external block of data, copying it might take essentially no time at all, but you could find that copying it after generating the data is massively slow. Only you know, and if you care about the performance, the only way to find out is to whack it in a for loop and time it.


If you worry about performance here, don't use std::vector<my_type>. Vector will copy all elements on every memory reallocation and can copy elements on element erasure from vector. Use boost::ptr_vector or std::vector<boost::shared_ptr>, this improves performance in both cases: adding elements to vector and reallocation/erasure.

EDIT:

I revised my answer:

The second approach has better performance because avoids copying of filled my_type instance (as opposite to default-constructed with empty std::vector member) on adding to vector. But it's less readable and less canonical. I would recommend to use the first approach as default one and only after profiling to selectively use the second approach or as - I previously proposed - to use boost::ptr_vector or std::vector<boost::shared_ptr>


Unless you give us more data, I think this depends on what your class contains and what data it has to generate. It's rather hard to tell which will be faster, as there could be things involved we cannot tell from your question.


It depends on exactly how the type is defined, and what the function you call does.

In both cases, the object m is copied into the vector after being constructed.

So the answer depends on whether generate_data_inside_self makes a copy more expensive or not. And that depends on how the assignment operator is defined. (And whether, in C++11, a move assignment operator exists, and whether you allow it to be called.)

But as always with performance questions, the only answer that matters is the one you get when you run the code. If you want to know which is faster, time the code and see for yourself.


The size of m is fixed in both examples. Any data you generate in generate_data_inside_self() is either just filling in holes or allocating space that vector doesn't care about (i.e. on the heap).

And more to the point, the content of that data is opaque from vector's perspective, so it doesn't effect performance if it happens to be all zeroes or a random assortment of values; the whole block of size sizeof(m) is copied either way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜