使用std :: vector的性能问题是什么? [英] what is the performance issue using std::vector

查看:199
本文介绍了使用std :: vector的性能问题是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







使用std :: vector会有什么性能损失?





问候,

Ranjith

Hi,


what is the performance penalty using std::vector?


Regards,
Ranjith

推荐答案

1。没有性能问题,直到你的实际应用程序运行得太慢。



2. std :: vector没有性能问题,除非你有一个应用程序运行得太慢,可以证明std :: vector占用了大量的处理时间。



3.我是不知道任何这样的真实世界应用程序遭受该特定问题。



4.警告:没有什么能阻止你使用 std :: vector 不恰当,或使用 std :: vector 当你应该使用e时。 G。 std :: list std :: map 代替!在我的上述陈述中,我假设您使用的数据类型不需要您在数组中插入或删除元素,至少不是定期的。
1. There is no performance issue until you have an actual application that runs too slowly.

2. There is no performance issue with std::vector until you have an application that runs too slowly, and can prove that std:: vector is taking up a significant amount of the processing time.

3. I am not aware of any such real world application suffering from that specific problem.

4. Caveat: nothing stops you from using std::vector inappropriately, or using std::vector when you should have been using e. g. std::list or std::map instead! In my above statements I am assuming you are using the type for data that don't require you to insert or remove elements in the midst of the array, at least not on a regular basis.


std :: vector非常适合在集合末尾插入和删除,也适用于随机读取访问。

文档更多地讨论它 - 向量类 [ ^ ]



以下是文档中的一些摘录 -



当随机访问性能非常高时,它们应该是序列的首选容器。



向量允许在序列末尾进行恒定时间插入和删除。在向量中间插入或删除元素需要线性时间。 deque类容器的性能在序列的开头和结尾处的插入和删除方面是优越的。列表类容器在序列中任何位置的插入和删除都是优越的。

std::vector is ideal for insertions and deletions at the end of the collection and also for random read access.
The documentation talks more about it - vector Class[^]

Here are some excerpts from the documentation -

They should be the preferred container for a sequence when random-access performance is at a premium.

Vectors allow constant time insertions and deletions at the end of the sequence. Inserting or deleting elements in the middle of a vector requires linear time. The performance of the deque Class container is superior with respect to insertions and deletions at the beginning and end of a sequence. The list Class container is superior with respect to insertions and deletions at any location within a sequence.


hi,

我从来没有遇到任何明显的性能命中,但是对于第二个意见查看此链接 [ ^ ]

特别是解释'reserve'的部分。



欢呼!

I never faced any noticeable performance hits, but for a second opinion take a look at this link[^]
specially the part where 'reserve' is explained.

cheers!


这篇关于使用std :: vector的性能问题是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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