添加到 C++ 中的集合之前或之后
鉴于
my_type m;
std::vector<my_type> v;
哪一个运行得更快?
m.generate_data_inside_self();
v.push_back(m);
或者,
v.push_back(m);
v[0].generate_data_inside_self();
如果向量保存指向 my_types 的指针,那么两者看起来大致相同。
然而,当像本例一样复制整个 my_type 对象时,我认为第二个会更快,因为要复制的数据较少,因为额外的数据仅在“m”位于“v”内之后才存在。
编辑:
在我的程序的示例中 my_type 看起来有点像这样。
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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当复制构造函数/运算符==的复杂度较小时添加它。如果您正在生成数据,很可能会增加复杂性,请在生成之前插入。
如果您有很多向量副本并且您关心性能,我的建议是拥有一个指针和新的向量(当然有一天删除) code>)对象并将它们放入
向量
中。这样,插入向量的成本不依赖于对象的复杂性。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 andnew
(and of course one daydelete
) the objects and put them in thevector
. That way, the cost of inserting invector
is not dependent on the complexity of the object.抱歉,但这很大程度上取决于您的类型。如果它保存指向某个大的外部数据块的指针,则复制它可能基本上不需要时间,但您可能会发现在生成数据后复制它的速度非常慢。只有您知道,如果您关心性能,找出答案的唯一方法就是在
for
循环中对其进行敲击并计时。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.如果您担心此处的性能,请不要使用
std::vector
。 Vector 将在每次内存重新分配时复制所有元素,并且可以在从向量中删除元素时复制元素。使用boost::ptr_vector
或std::vector
,这可以提高两种情况下的性能:向向量添加元素以及重新分配/擦除。编辑:
我修改了我的答案:
第二种方法具有更好的性能,因为避免了复制 filled
my_type
实例(与默认构造的空实例相反)std::vector
成员)添加到向量。但它的可读性和规范性较差。我建议使用第一种方法作为默认方法,并且仅在分析后选择性地使用第二种方法,或者像我之前建议的那样使用boost::ptr_vector
或std::vector< boost::shared_ptr>
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. Useboost::ptr_vector
orstd::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 emptystd::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 useboost::ptr_vector
orstd::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.
这取决于类型的定义方式以及您调用的函数的作用。
在这两种情况下,对象
m
在构造后都会被复制到向量中。因此,答案取决于
generate_data_inside_self
是否会使副本变得更昂贵。这取决于赋值运算符的定义方式。 (以及在 C++11 中是否存在移动赋值运算符,以及是否允许调用它。)但与性能问题一样,唯一重要的答案是运行代码时得到的答案。如果您想知道哪个更快,请对代码进行计时并亲自查看。
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.
在两个示例中,
m
的大小都是固定的。在generate_data_inside_self()
中生成的任何数据要么只是填充空洞,要么分配vector
不关心的空间(即在堆上)。更重要的是,从向量的角度来看,该数据的内容是不透明的,因此如果它恰好是全零或随机组合的值,则不会影响性能;无论哪种方式都会复制大小为
sizeof(m)
的整个块。The size of
m
is fixed in both examples. Any data you generate ingenerate_data_inside_self()
is either just filling in holes or allocating space thatvector
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 sizesizeof(m)
is copied either way.