为什么std::vector可能比原始动态分配的数组快?

Why might std::vector be faster than a raw dynamically allocated array?

本文关键字:动态分配 数组 原始 std vector 为什么      更新时间:2023-10-16

与同事讨论的结果,我最终编写了测试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

有三件事是清楚的:

  1. 数组在用户时间上更快
  2. 矢量更快更少的系统时间
  3. Over all vector赢得了这场战斗

问题是"为什么?"

我猜系统时间问题一定与页面错误有关,但我不能准确地描述为什么一个人会有更多的页面错误。

至于用户时间问题,我不太感兴趣,但我也很想知道大家对此的看法。我曾想象它与初始化有关,尽管我没有将初始化值传递给向量构造函数,所以我不知道。

与动态数组相比,差异不在于向量的性能,而在于您执行的内存访问次数。

实际上,在向量测试中,你是在重新访问缓存的内存,而在数组版本中,你不这样做。在这两种情况下,你都要为缓存vector版本付出代价。

在向量测试中,您为数组分配动态内存,但不触及它,内存从未被触及,由于该操作没有页面错误。vector被创建,初始化,然后第二次遍历将访问已经缓存的数据(如果大小适合缓存,如果不适合,则不在缓存中,但两个版本的开销相同)。

另一方面,在测试数组时,vector构造函数初始化元素,这意味着在试图分析数组行为的情况下,遍历vector内容,遍历数组元素。内存访问次数、页面错误次数和应用程序使用的内存次数加倍。

您可以尝试修改代码,以便像这样执行动态分配:

int * intArray = new int[ numInts ](); // note extra ()

值初始化整个数组,或者初始化数组内容。运行修改后的测试版本的结果应该是相似的。

您是否多次运行测试?基准测试是一个艰难的过程,必须依靠平均值才能得到任何有意义的结果;在运行数组基准测试时,有可能有几个CPU周期专门用于其他事情,从而减慢了它的速度。我希望给出足够的结果,他们会是相似的,因为std::vector是用c风格的数组在它的核心编写的。