在我们的c++课程中,他们建议不要再在新项目中使用c++数组。据我所知,Stroustroup本人建议不要使用数组。但是否存在显著的性能差异?
当前回答
当你想要一个未初始化的缓冲区(例如用作memcpy()的目标)时,使用std::vector与使用raw数组肯定会有性能影响。vector将使用默认构造函数初始化其所有元素。原始数组则不会。
c++规范中std:vector构造函数接受count参数(这是第三种形式):
从各种数据源构造一个新容器,可选地使用用户提供的分配器alloc。
使用默认插入的t的count个实例构造容器。
复杂性
2-3)计数线性
原始数组不会产生这种初始化代价。
注意,使用自定义分配器,可以避免vector元素的“初始化”(即使用默认初始化而不是值初始化)。请看这些问题了解更多细节:
这是c++ 11和Boost下vector::resize(size_type n)的行为吗?容器正确吗? 如何避免std::vector<>来初始化它的所有元素?
其他回答
STL is a heavily optimized library. In fact, it's even suggested to use STL in games where high performance might be needed. Arrays are too error prone to be used in day to day tasks. Today's compilers are also very smart and can really produce excellent code with STL. If you know what you are doing, STL can usually provide the necessary performance. For example by initializing vectors to required size (if you know from start), you can basically achieve the array performance. However, there might be cases where you still need arrays. When interfacing with low level code (i.e. assembly) or old libraries that require arrays, you might not be able to use vectors.
应该避免使用带有new的c++数组(即使用动态数组)。这里有一个问题,你必须跟踪大小,你需要手动删除它们,做各种各样的家务。
在堆栈上使用数组也是不鼓励的,因为您没有范围检查,并且传递数组将丢失关于其大小的任何信息(数组到指针的转换)。在这种情况下,应该使用std::array,它将c++数组包装在一个小类中,并提供一个size函数和迭代器来迭代它。
现在,std::vector vs.原生c++数组(取自互联网):
// Comparison of assembly code generated for basic indexing, dereferencing,
// and increment operations on vectors and arrays/pointers.
// Assembly code was generated by gcc 4.1.0 invoked with g++ -O3 -S on a
// x86_64-suse-linux machine.
#include <vector>
struct S
{
int padding;
std::vector<int> v;
int * p;
std::vector<int>::iterator i;
};
int pointer_index (S & s) { return s.p[3]; }
// movq 32(%rdi), %rax
// movl 12(%rax), %eax
// ret
int vector_index (S & s) { return s.v[3]; }
// movq 8(%rdi), %rax
// movl 12(%rax), %eax
// ret
// Conclusion: Indexing a vector is the same damn thing as indexing a pointer.
int pointer_deref (S & s) { return *s.p; }
// movq 32(%rdi), %rax
// movl (%rax), %eax
// ret
int iterator_deref (S & s) { return *s.i; }
// movq 40(%rdi), %rax
// movl (%rax), %eax
// ret
// Conclusion: Dereferencing a vector iterator is the same damn thing
// as dereferencing a pointer.
void pointer_increment (S & s) { ++s.p; }
// addq $4, 32(%rdi)
// ret
void iterator_increment (S & s) { ++s.i; }
// addq $4, 40(%rdi)
// ret
// Conclusion: Incrementing a vector iterator is the same damn thing as
// incrementing a pointer.
注意:如果你用new分配数组,并分配非类对象(如纯int)或没有用户定义的构造函数的类,并且你不想让你的元素初始化,使用new-allocated数组可以有性能优势,因为std::vector在构造时将所有元素初始化为默认值(例如int为0)(感谢@bernie提醒我)。
我认为最主要的问题不是性能,而是安全性。使用数组可能会犯很多错误(例如,考虑调整大小),而使用向量可以省去很多麻烦。
如果不需要动态调整大小,则会有存储容量的内存开销(一个指针/size_t)。就是这样。
假设一个固定长度的数组(例如int* v = new int[1000];vs std::vector<int> v(1000);, v的大小保持固定在1000),唯一真正重要的性能考虑因素(或者至少对我来说,当我处于类似的困境时)是访问元素的速度。我查了一下STL的向量代码,下面是我的发现:
const_reference
operator[](size_type __n) const
{ return *(this->_M_impl._M_start + __n); }
这个函数肯定会被编译器内联。所以,只要你计划对v做的唯一一件事就是用operator[]访问它的元素,看起来在性能上不应该有任何差别。
推荐文章
- cbegin/cend背后的原因是什么?
- 如何在Eclipse CDT中启用c++ 11/ c++ 0x支持?
- 将string (or char*)转换为wstring (or wchar_t*)
- 如何停止c++控制台应用程序从退出立即?
- 有没有替换Windows (Visual C)的unistd.h ?
- 目标文件中无法解析的外部符号
- 在Java中创建对象数组
- Lambda捕获作为const引用?
- 如何插入元素到数组在特定的位置?
- 如何从c风格数组初始化std::vector ?
- 是std::vector复制对象push_back?
- 是否有一个原因,Swift数组赋值是不一致的(既不是引用也不是深度复制)?
- 在Bash中将命令行参数转换为数组
- 如何创建数组列表的数组?
- 为什么在c++ 11中使用非成员begin和end函数?