我想在float、double和long double上运行谷歌/基准测试。
考虑到BENCHMARK_TEMPLATE 示例,我尝试了以下操作:
#include <cmath>
#include <ostream>
#include <random>
#include <benchmark/benchmark.h>
template<typename Real>
BM_PowTemplated(benchmark::State& state) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<Real> dis(1, 10);
auto s = dis(gen);
auto t = dis(gen);
Real y;
while (state.KeepRunning()) {
benchmark::DoNotOptimize(y = std::pow(s, t));
}
std::ostream cnull(0);
cnull << y;
}
BENCHMARK_TEMPLATE(BM_PowTemplated, float, double, long double);
BENCHMARK_MAIN();我以为这会为float、double和long double创建三个基准,但是它没有编译!
创建模板化的google基准测试的正确语法是什么?我对BENCHMARK_TEMPLATE应该如何工作的心智模型是否正确,如果正确,我如何修复这段代码?
发布于 2016-06-24 23:34:12
您错误地使用了BENCHMARK_TEMPLATE
BENCHMARK_TEMPLATE(BM_PowTemplated, float, double, long double);https://github.com/google/benchmark/的自述文件说
template <class Q> int BM_Sequential(benchmark::State& state) { .. }
BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
Three macros are provided for adding benchmark templates.
#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters.
#define BENCHMARK_TEMPLATE1(func, arg1)
#define BENCHMARK_TEMPLATE2(func, arg1, arg2)因此,BENCHMARK_TEMPLATE和arg1用于一个模板参数的函数,arg1和arg2用于两个模板参数的函数。您的BM_PowTemplated只有一个参数,所以不能在3个args中使用BENCHMARK_TEMPLATE。
查看google的test/cxx03_test.cc /benchmark,例如:test.cc
template <class T, class U>
void BM_template2(benchmark::State& state) {
BM_empty(state);
}
BENCHMARK_TEMPLATE2(BM_template2, int, long);
template <class T>
void BM_template1(benchmark::State& state) {
BM_empty(state);
}
BENCHMARK_TEMPLATE(BM_template1, long);
BENCHMARK_TEMPLATE1(BM_template1, int);PS:宏定义:api.h#L684
我们可以看到宏的a (a, b)参数在<和>中,它们被用作f< a,b >。
// template<int arg>
// void BM_Foo(int iters);
//
// BENCHMARK_TEMPLATE(BM_Foo, 1);
//
// will register BM_Foo<1> as a benchmark.
#define BENCHMARK_TEMPLATE1(n, a) \
BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
#define BENCHMARK_TEMPLATE2(n, a, b) \
BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
new ::benchmark::internal::FunctionBenchmark( \
#n "<" #a "," #b ">", n<a, b>)))
#define BENCHMARK_TEMPLATE(n, ...) \
BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \
new ::benchmark::internal::FunctionBenchmark( \
#n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))因此,BENCHMARK_TEMPLATE不能迭代多个变体并从一行中定义多个变体。
https://stackoverflow.com/questions/38023017
复制相似问题