我正在运行谷歌基准测试以进行一些基本的缓存测试,但出现以下错误:
terminate called after throwing an instance of 'std::length_error'
what(): cannot create std::vector larger than max_size()
但是,我正在打印 max_size 和实际尺寸(见下文),而 max_size 等于 2^60-1,它在 2^28 处中断。我错过了什么?
基准代码如下。代码使用 Clang 11 和 c++20 编译。
static void bm_std_vector_in_cache_double(benchmark::State& state)
{
auto constexpr d{3.1415};
auto const bytes = (2 << state.range(0)) * 1024;
auto data = std::vector<double>(bytes / sizeof(double), d);
std::cout << data.max_size() << '\n';
std::cout << data.size() << '\n';
for (auto _ : state){
auto sum = 0.0;
for(auto j = 0; j < data.size(); ++j)
benchmark::DoNotOptimize(sum += data[j] * data[j]);
}
state.SetBytesProcessed(state.iterations() * data.size());
}
BENCHMARK(bm_std_vector_in_cache_double)->DenseRange(1, 20);