【发布时间】:2013-08-15 16:46:54
【问题描述】:
在阅读了这篇关于列表缓存不友好的博文后: http://www.baptiste-wicht.com/2012/11/cpp-benchmark-vector-vs-list/
...我试图通过将实际对象放入每个节点(从而删除一个间接操作)来使指向对象的指针的 std::list 对缓存更友好,希望当当前节点被缓存时,对象也会。但是,性能实际上下降了。这是我使用的代码:
来源和二进制文件: http://wilcobrouwer.nl/bestanden/ListTest%202013-8-15%20%233.7z
#include <list>
using std::list;
list<Object*> case1;
list<Object> case2;
class Object {
public:
Object(char i);
~Object();
char dump[256];
};
// Should not notice much of a difference here, equal amounts of memory are
// allocated
void Insertion(Test* test) {
// create object, copy pointer
float start1 = clock->GetTimeSec();
for(int i = 0;i < test->size;i++) {
case1.push_back(new Object(i));
}
test->insertion1 = clock->GetTimeSec()-start1;
// create object in place, no temps on stack
float start2 = clock->GetTimeSec();
for(int i = 0;i < test->size;i++) {
case2.emplace_back(i);
}
test->insertion2 = clock->GetTimeSec()-start2;
}
// Case 2 removes one extra layer of derefence, so it should be more cache
// friendly, because when the list node is found in cache, the object should be
// there too
void Iteration(Test* test) {
// faster than case2 for some reason
float start1 = clock->GetTimeSec();
int tmp1 = 0;
for(list<Object*>::iterator i = case1.begin();i != case1.end();i++) {
tmp1 += (**i).dump[128];
}
test->iteration1 = clock->GetTimeSec()-start1;
// why the hell is this slower? I removed a dereference
float start2 = clock->GetTimeSec();
int tmp2 = 0;
for(list<Object>::iterator i = case2.begin();i != case2.end();i++) {
tmp2 += (*i).dump[128]; // is equal to tmp1, so no mistakes...
}
test->iteration2 = clock->GetTimeSec()-start2;
}
// Case 2 removes one extra layer of derefence, so it should be more cache
// friendly, because when the list node is found in cache, the object should be
// there too
void Deletion(Test* test) {
// again, faster than case2 for some reason
float start1 = clock->GetTimeSec();
int size1 = case1.size();
for(list<Object*>::iterator i = case1.begin();i != case1.end();i++) {
delete *i;
}
case1.clear();
test->deletion1 = clock->GetTimeSec()-start1;
// as before: why is this slower? I removed a dereference
float start2 = clock->GetTimeSec();
int size2 = case2.size();
case2.clear();
test->deletion2 = clock->GetTimeSec()-start2;
}
这些函数针对从 1 到 100000 线性变化的 test->size 值运行,并且时钟->GetTimeSec() 之间的时间差在 计算完成后保存到磁盘。可以在这里找到我的结果图:
http://wilcobrouwer.nl/bestanden/ListTestFix.png
如您所见,案例 2 在插入和删除时快了大约 10%,但在迭代时慢了大约 10%,这意味着迭代案例 1 所需的额外取消引用使其更快!
我在这里错过了什么?
编辑 1: 我的 CPU 是 Phenom II X4 @ 3.5GHz(恒定频率),具有 64K/1MB/6MB 缓存,我正在以这种方式编译(请注意 -m64 是暗示,这意味着通过 -mfpmath=ssse 禁止 x87):
Compiler: TDM-GCC 4.7.1 64-bit Release
rm -f obj/Clock.o obj/main.o obj/Object.o ListTest.exe
g++.exe -c Clock.cpp -o obj/Clock.o -std=gnu++11
g++.exe -c main.cpp -o obj/main.o -std=gnu++11
g++.exe -c Objecst.cpp -o obj/Object.o -std=gnu++11
g++.exe obj/Clock.o obj/main.o obj/Object.o -o ListTest.exe -static-libgcc
编辑 2: 对 Dale Wilson 的回答:使用列表我的意思是 std::list。对 Mats Petersson 的回答:图片中添加了摘要。优化检查正在进行中。回答询问更大数据集的人:抱歉,我只有 4GiB 的 RAM,而且从当前最大值到填充的图非常无聊。
编辑 3:我启用了 -O3(-O2 产生类似的结果),这只会使事情变得更糟:
http://wilcobrouwer.nl/bestanden/ListTestO3Fix.png
这一次,案例 2 在插入和删除时快了大约 20%,但这次在迭代时慢了大约 1~5 倍(在更大的测试规模下变得更糟)。同样的结论。
编辑 4: 对 Maxim Yegorushkin 的回答:CPU 频率缩放恰好被禁用(忘了提),我的 CPU 始终运行在 3.5GHz。此外,基本上也可以从更多测试中选择平均值或最佳结果,因为 x 轴上有足够多的样本点。也启用了优化:-O3、-m64 和 mfpmath=sse 都已设置。将相同的测试一个接一个地添加到 std::vector 测试(检查源代码)并没有显着改变。
修改5:修复了一些错别字(删除结果没有显示,但迭代结果显示了两次。这已经清除了删除问题,但迭代问题仍然存在。
【问题讨论】:
-
我们假设使用标准吗?所以 list 实际上是 std::list?
-
你记得启用编译器优化吗?
-
添加 -O2 或 -O3 并再次检查....
-
您对缓存的假设有点错误。包含大对象的节点对缓存不太友好,因为它们不适合单个缓存行。指针列表可能适合同一缓存行中的两个或多个节点,这可能会加速迭代 - 可能超过额外间接的成本。
-
@Smac89 为什么迭代会涉及任何内存分配?