为什么std::vector可能比原始动态分配的数组快?
Why might std::vector be faster than a raw dynamically allocated array?
与同事讨论的结果,我最终编写了测试std::vector
与原始动态分配数组的基准测试,并以一个惊喜结束。
#include "testconsts.h" // defines NUM_INTS across all tests
#include <vector>
int main()
{
const int numInts = NUM_INTS;
std::vector<int> intVector( numInts );
int * const intArray = new int[ numInts ];
++intVector[0]; // force access to affect optimization
++intArray[0]; // force access to affect optimization
for( int i = 0; i < numInts; ++i )
{
++intArray[i];
}
delete[] intArray;
return 0;
}
:
#include "testconsts.h" // defines NUM_INTS across all tests
#include <vector>
int main()
{
const int numInts = NUM_INTS;
std::vector<int> intVector( numInts );
int * intArray = new int[ numInts ];
++intArray[0]; // force access to affect optimization
++intVector[0]; // force access to affect optimization
for( int i = 0; i < numInts; ++i )
{
++intVector[i];
}
delete[] intArray;
return 0;
}
它们是用g++ -O3和gcc 4.4.3编译的
使用time
多次运行基准测试的结果类似于:
real 0m0.757s
user 0m0.176s
sys 0m0.588s
向量:real 0m0.572s
user 0m0.268s
sys 0m0.304s
有三件事是清楚的:
- 数组在用户时间上更快
- 矢量更快更少的系统时间
- Over all vector赢得了这场战斗
问题是"为什么?"
我猜系统时间问题一定与页面错误有关,但我不能准确地描述为什么一个人会有更多的页面错误。
至于用户时间问题,我不太感兴趣,但我也很想知道大家对此的看法。我曾想象它与初始化有关,尽管我没有将初始化值传递给向量构造函数,所以我不知道。
与动态数组相比,差异不在于向量的性能,而在于您执行的内存访问次数。
实际上,在向量测试中,你是在重新访问缓存的内存,而在数组版本中,你不这样做。在这两种情况下,你都要为缓存vector版本付出代价。
在向量测试中,您为数组分配动态内存,但不触及它,内存从未被触及,由于该操作没有页面错误。vector被创建,初始化,然后第二次遍历将访问已经缓存的数据(如果大小适合缓存,如果不适合,则不在缓存中,但两个版本的开销相同)。
另一方面,在测试数组时,vector构造函数初始化元素,这意味着在试图分析数组行为的情况下,遍历vector内容,遍历数组元素。内存访问次数、页面错误次数和应用程序使用的内存次数加倍。您可以尝试修改代码,以便像这样执行动态分配:
int * intArray = new int[ numInts ](); // note extra ()
将值初始化整个数组,或者初始化数组内容。运行修改后的测试版本的结果应该是相似的。
您是否多次运行测试?基准测试是一个艰难的过程,必须依靠平均值才能得到任何有意义的结果;在运行数组基准测试时,有可能有几个CPU周期专门用于其他事情,从而减慢了它的速度。我希望给出足够的结果,他们会是相似的,因为std::vector
是用c风格的数组在它的核心编写的。
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 动态更改数组的大小并读取值.(无矢量)
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 动态维度数组的 C++ 别名指针
- C++ 在析构函数调用之前删除的动态成员数组
- 从类 c++ 动态创建数组
- 具有静态 std::array 的神经网络比使用动态 C 数组的神经网络慢
- "Error reading character of string" 为哈希动态增长数组的问题 (C++)
- 为什么我们在传递动态 2D 数组时不需要列数?
- 如何解决在使用动态 2D 数组进行矩阵乘法的 MPI 进行并行编程时的问题
- 使用动态布尔数组时出现问题
- C++ 在类中删除动态 2D 数组时不断"Aborted (core dumped)"
- 如何在C 中动态输入数组
- 关于删除动态对象数组
- 在C 中动态更改数组大小
- 使用动态指针数组进行动态对象分配 - 使用什么删除?
- 动态字符串数组分配错误
- 函数中的动态 2D 数组分配并将其返回给主数组
- c++动态字符串数组分配
- 使用字符串或字符动态分配(数组)