C ++容器的性能问题 [英] C++ Container performance question

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

问题描述

什么有更好的表现,一个STL的vector,或动态数组,这只是realloc'd每次我要什么补充呢?

请问使用向量:迭代更快,然后使用for循环阵列上?

如果有人可以解释为什么,那将是巨大的。

解决方案
  

什么有更好的表现,一个STL的vector,或动态数组,这只是realloc'd每次我要什么补充呢?

STL载体具有插入的分期常量时间的(因为重新分配没有完成所有的时间和保留通过发生1.5倍(最低))。

因此​​,根据你的描述,载体将大规模重新分配比所有的时间

更快
  

请问使用向量:迭代更快,然后使用for循环阵列上?

在一般情况下:完全相同。 然而某些STL实现生成在调试模式下检查,能大大减缓使用容器的迭代器。

(注意,大多数实现实现矢量/字符串迭代器作为一个typedef到的value_type *

如果有人可以解释为什么,那将是巨大的。

What would have better performance, a stl vector, or a dynamic array that's just realloc'd everytime I want to add something to it?

Would using vectors::iterator be faster then using a for loop on an array?

And if someone could explain why, that would be great.

解决方案

What would have better performance, a stl vector, or a dynamic array that's just realloc'd everytime I want to add something to it?

Stl vectors have insertion in amortized constant time (because reallocation is not done all the time and reservations occur by factor 1.5 (minimum)).

Therefore according to your description, vectors will be massively faster than reallocating all the time

Would using vectors::iterator be faster then using a for loop on an array?

In the general case: exactly identical. However certain STL implementations generate checks in debug mode that can considerably slow down the use of container iterators.

(note most implementations implement vector/string iterators as a typedef to value_type*)

And if someone could explain why, that would be great.

这篇关于C ++容器的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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