这是我尝试过的一个循环,std::vector<double>并且使用普通的旧版本double*.
对于1000万个元素,矢量版本始终在该double*版本所占用的时间的80%左右运行; 几乎任何价值N,矢量明显更快.
偷看GCC STL源代码,我没有看到它std::vector正在做的事情比double*成语正在做的更好(即用普通的旧分配new[],operator[]取消引用偏移量). 这个问题也说明了这一点.
任何想法为什么矢量版本更快?
Compiler: GCC 4.6.1
Example compile line: g++ -Ofast -march=native -DNDEBUG \
-ftree-vectorizer-verbose=2 -o vector.bin \
vector.cpp -lrt
OS: CentOS 5
CPU: Opteron 8431
RAM: 128 GB
Run Code Online (Sandbox Code Playgroud)
如果我使用icpc 11.1或在Xeon上运行,结果在质量上是相同的.此外,矢量化器转储表示只有std::vector向量化的构造函数中的填充操作.
矢量版本:
#include <vector>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include "util.h"
#include "rkck_params.h"
using namespace std;
int main( int argc, char* argv[] )
{
const size_t N = …Run Code Online (Sandbox Code Playgroud)