vector ::插入性能提示。 [英] vector::insert performance tip.

查看:57
本文介绍了vector ::插入性能提示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



试图将一个集合< int(s)转储到一个向量中(v)我的猜测是

v.insert(v.end() ,s.begin(),s.end())

至少与

copy(s.begin(),s.end()一样有效, back_inserter(v))

事实证明它几乎慢了两倍(即使没有在复制之前使用

reserve())这是因为它遍历了设置两次。

我将它作为常规发布(通常是正确的)

建议

优先选择会员功能算法。


问候

Kostas

Hi,
Trying to dump a set<int(s) in a vector (v) my guess was that
v.insert(v.end(),s.begin(),s.end())
would be at least as efficient as
copy(s.begin(), s.end(), back_inserter(v))
It turn out that it is almost two times slower (even without using
reserve() before copy) and that''s because it traverses the set twice.
I''m posting it as an exception to the usual (and usually correct)
advise
"Prefer member functions to algorithms".

Regards
Kostas

推荐答案

kostas写道:
kostas wrote:

试图将一个集合< int(s)转储到一个向量中(v)我的猜测是

v。 insert(v.end(),s.begin(),s.end())

至少与

copy(s.begin()一样有效,s.end(),back_inserter(v))

事实证明它几乎慢了两倍(甚至没有在复制之前使用

reserve())那是因为它遍历集合两次。
Trying to dump a set<int(s) in a vector (v) my guess was that
v.insert(v.end(),s.begin(),s.end())
would be at least as efficient as
copy(s.begin(), s.end(), back_inserter(v))
It turn out that it is almost two times slower (even without using
reserve() before copy) and that''s because it traverses the set twice.



我不明白为什么需要两次遍历集合。任何提示

为什么会发生这种情况?

I don''t see why it would need to traverse the set twice. Any hint
to why it was happening?


我将它作为常规发布(通常是正确的)

建议

优先考虑算法的成员函数。
I''m posting it as an exception to the usual (and usually correct)
advise
"Prefer member functions to algorithms".



我不认为这个建议与性能有任何关系。

通常(并且总是正确的)建议WRT性能是首先测量

然后尝试优化。


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复请不要问

I don''t think the advice has anything to do with performance. The
usual (and always correct) advice WRT performance is "measure first
then try to optimize".

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


4月28日晚上10点49分,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Apr 28, 10:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

我不明白为什么需要两次遍历集合。任何暗示

为什么会发生这种情况?
I don''t see why it would need to traverse the set twice. Any hint
to why it was happening?



STL的实现我首先要用它来计算

的距离

s.end() - s.begin()以保留适当的空间。这是

不是

vector :: insert的标准(或通常)实现?

The implementation of STL i use it needs first to calculate the
distance
s.end() - s.begin() in order to reserve the appropriate space. Is this
not
the standard (or the usual) implementation of vector::insert ?


I不要认为这些建议与表现有任何关系。
I don''t think the advice has anything to do with performance.



用什么呢?

with what then?


2007年4月28日14:32:55 -0700 ,kostas< sk ****** @ gmail.comwrote:
On 28 Apr 2007 14:32:55 -0700, kostas <sk******@gmail.comwrote:

> 4月28日,10:49 pm,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
>On Apr 28, 10:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

>我不知道为什么需要两次遍历该集合。任何提示为什么会发生?
>I don''t see why it would need to traverse the set twice. Any hint
to why it was happening?


STL的实现我用它首先需要计算
距离s.end() - s.begin()才能保留相应的
空间。这不是vector :: insert的标准(或通常)实现吗?


The implementation of STL i use it needs first to calculate the
distance s.end() - s.begin() in order to reserve the appropriate
space. Is this not the standard (or the usual) implementation
of vector::insert ?



这可能是''正常''实现,对于

随机访问迭代器更快,但在某些情况下更慢双向

迭代器。我想你的元素数量非常少。否则

复制(...,back_inserter(v))变体中的向量重定位将会比双遍遍一次耗费更多时间。

-

Roland Pibinger

最好的软件简单,优雅,充满戏剧性 - Grady Booch

It''s probably the ''normal'' implementation which is faster for
random-access iterators but slower in some cases of bidirectional
iterators. I guess your number of elements is very small. Otherwise
the vector relocations in the copy(...,back_inserter(v)) variant would
be more time-consuming that the double traversal once.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch


这篇关于vector ::插入性能提示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆