我发现迭代会更快地通过向量
而不是使用变量(i)来计算std::vector<T>::iterator
。
感谢您的一些评论,这里有一些额外的信息:(1) 我使用Visual Studio C ++编译器; (2)我在发布模式下编译并使用优化-O2:)
如果变量i递增,则迭代采用
5875ms:
std::vector<Data> vec(MAX_DATA);
stopWatch.start();
for (unsigned i = 0U; i < MAX_DATA; ++i) {
vec[i].x = 0;
vec[i].y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data are stored in memory next to each other");
或5723ms:
std::vector<Data*> vec2;
for (unsigned i = 0U; i < MAX_DATA; ++i)
vec2.push_back(new Data());
stopWatch.start();
for (unsigned i = 0U; i < MAX_DATA; ++i) {
vec2[i]->x = 0;
vec2[i]->y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data is in memory at a random position");
如果std::vector<Data>::Iterator
用于迭代,则迭代将采用
29ms:
std::vector<Data> vec(MAX_DATA);
stopWatch.start();
for (auto& it : vec) {
it.x = 0;
it.y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data are stored in memory next to each other");
或110ms:
std::vector<Data*> vec2;
for (unsigned i = 0U; i < MAX_DATA; ++i)
vec2.push_back(new Data());
stopWatch.start();
for (auto& it : vec2) {
it->x = 0;
it->y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data is in memory at a random position");
为什么另一次迭代要快得多?
我想知道使用变量i进行迭代,其中数据位于存储器中的不同位置,与使用变量i的迭代一样快,其中数据并置在存储器中。
数据在内存中彼此相邻的事实应该减少缓存未命中,并且与std::vector<Data>::Iterator
的迭代一起使用,为什么不与另一个迭代?
或者我是否敢于和29到110毫秒的距离不是债务缓存未命中?
整个程序看起来像这样:
#include <iostream>
#include <chrono>
#include <vector>
#include <string>
class StopWatch
{
public:
void start() {
this->t1 = std::chrono::high_resolution_clock::now();
}
void stop() {
this->t2 = std::chrono::high_resolution_clock::now();
this->diff = t2 - t1;
}
void printSpanAsMs(std::string startText = "time span") {
long diffAsMs = std::chrono::duration_cast<std::chrono::milliseconds>
(diff).count();
std::cout << startText << ": " << diffAsMs << "ms" << std::endl;
}
private:
std::chrono::high_resolution_clock::time_point t1, t2;
std::chrono::high_resolution_clock::duration diff;
} stopWatch;
struct Data {
int x, y;
};
const unsigned long MAX_DATA = 20000000;
void test1()
{
std::cout << "1. Test \n Use i to iterate through the vector" <<
std::endl;
std::vector<Data> vec(MAX_DATA);
stopWatch.start();
for (unsigned i = 0U; i < MAX_DATA; ++i) {
vec[i].x = 0;
vec[i].y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data are stored in memory next to each
other");
//////////////////////////////////////////////////
std::vector<Data*> vec2;
for (unsigned i = 0U; i < MAX_DATA; ++i)
vec2.push_back(new Data());
stopWatch.start();
for (unsigned i = 0U; i < MAX_DATA; ++i) {
vec2[i]->x = 0;
vec2[i]->y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data is in memory at a random position");
for (unsigned i = 0U; i < MAX_DATA; ++i) {
delete vec2[i];
vec2[i] = nullptr;
}
}
void test2()
{
std::cout << "2. Test \n Use std::vector<T>::iteraror to iterate through
the vector" << std::endl;
std::vector<Data> vec(MAX_DATA);
stopWatch.start();
for (auto& it : vec) {
it.x = 0;
it.y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data are stored in memory next to each
other");
//////////////////////////////////////////////////
std::vector<Data*> vec2;
for (unsigned i = 0U; i < MAX_DATA; ++i)
vec2.push_back(new Data());
stopWatch.start();
for (auto& it : vec2) {
it->x = 0;
it->y = 0;
}
stopWatch.stop();
stopWatch.printSpanAsMs("The data is in memory at a random position");
for (auto& it : vec2) {
delete it;
it = nullptr;
}
}
int main()
{
test1();
test2();
system("PAUSE");
return 0;
}
答案 0 :(得分:1)
为什么另一次迭代要快得多?
原因是MSVC 2017无法正确优化它。
在第一种情况下,它完全无法优化循环:
for (unsigned i = 0U; i < MAX_DATA; ++i) {
vec[i].x = 0;
vec[i].y = 0;
}
生成的代码(live demo):
xor r9d, r9d
mov eax, r9d
$LL4@test1:
mov rdx, QWORD PTR [rcx]
lea rax, QWORD PTR [rax+16]
mov DWORD PTR [rax+rdx-16], r9d
mov rdx, QWORD PTR [rcx]
mov DWORD PTR [rax+rdx-12], r9d
mov rdx, QWORD PTR [rcx]
mov DWORD PTR [rax+rdx-8], r9d
mov rdx, QWORD PTR [rcx]
mov DWORD PTR [rax+rdx-4], r9d
sub r8, 1
jne SHORT $LL4@test1
将unsigned i
替换为size_t i
或将索引访问权限提升为参考资料并不提供帮助(demo)。
唯一有用的是使用像你已经发现的迭代器:
for (auto& it : vec) {
it.x = 0;
it.y = 0;
}
生成的代码(live demo):
xor ecx, ecx
npad 2
$LL4@test2:
mov QWORD PTR [rax], rcx
add rax, 8
cmp rax, rdx
jne SHORT $LL4@test2
在这两种情况下, clang只会调用memset
。
故事的寓意:如果你关心表现,看看生成的代码。向供应商报告问题。