【发布时间】:2019-03-30 10:35:22
【问题描述】:
- 为什么
posix_memalign写成::posix_memalign? - 这里的
memory是什么?
我希望对我的高速缓存和 RAM 的读写速度进行基准测试。为此,我想使用谷歌基准库,我看到了一个使用它的示例代码。或多或少我明白了代码的概念,但memory 站在这里是什么?为什么我们将它作为指向 void 的指针?另外,为什么这个例子写posix_memalign 和::?是不是因为我们引用的是google benchmark class?
#include <cstddef>
#include <cstdlib>
#include <string.h>
#include <emmintrin.h>
#include <immintrin.h>
#include "benchmark/benchmark.h"
#define ARGS \
->RangeMultiplier(2)->Range(1024, 2*1024*1024) \
->UseRealTime()
template <class Word>
void BM_write_seq(benchmark::State& state) {
void* memory;
if (::posix_memalign(&memory, 64, state.range_x()) != 0) return;
void* const end = static_cast<char*>(memory) + state.range_x();
Word* const p0 = static_cast<Word*>(memory);
Word* const p1 = static_cast<Word*>(end);
Word fill; ::memset(&fill, 0xab, sizeof(fill));
while (state.KeepRunning()) {
for (Word* p = p0; p < p1; ++p) {
benchmark::DoNotOptimize(*p = fill);
}
}
::free(memory);
}
【问题讨论】:
-
这个问题怎么重复?
标签: c++ microbenchmark google-benchmark