C++ 谷歌基准代码设置
给定以下代码C++ 谷歌基准代码设置,c++,google-benchmark,C++,Google Benchmark,给定以下代码 #include <benchmark/benchmark.h> #include <iostream> static void BM_foo(benchmark::State& state) { std::cout << "Foo "<< std::endl; for (auto _: state) { std::cout << state.iterations() << " I
#include <benchmark/benchmark.h>
#include <iostream>
static void BM_foo(benchmark::State& state) {
std::cout << "Foo "<< std::endl;
for (auto _: state) {
std::cout << state.iterations() << " In loop " <<std::endl;
}
}
BENCHMARK(BM_foo);
BENCHMARK_MAIN();
问题:
State::KeepRunningBatch
conn.IsReady()
处于循环中它看起来很像你需要的是一个评测库,而不是一个miro benchamraking库“std::我们是否可以多次调用整个基准函数,用不同的迭代计数控制内部循环。如果您的内部循环更昂贵,您将看到Foo打印的次数更少,因为我们可以更快地达到最低运行时间。它按预期工作。@dma:如果序言也很昂贵,我不想花时间重新运行它怎么办?如何确保序言只运行一次?
DBConnection* conn; // global variables, so that each thread can run access it.
static void BM_db_insert(benchmark::State& state) {
if (state.thread_index == 0) {
# only let 1 thread to setup the DBConnections
conn = openDBConn();
}
DBSession* session = conn.openSession();
for (auto _ : state) {
session.insertOp(id, ..);
}
session.close();
if (state.thread_index == 0) {
conn.close();
}
}