首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >google/benchmark: BENCHMARK_TEMPLATE语法

google/benchmark: BENCHMARK_TEMPLATE语法
EN

Stack Overflow用户
提问于 2016-06-24 22:35:22
回答 1查看 3.1K关注 0票数 6

我想在floatdoublelong double上运行谷歌/基准测试。

考虑到BENCHMARK_TEMPLATE 示例,我尝试了以下操作:

代码语言:javascript
复制
#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应该如何工作的心智模型是否正确,如果正确,我如何修复这段代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-24 23:34:12

您错误地使用了BENCHMARK_TEMPLATE

代码语言:javascript
复制
BENCHMARK_TEMPLATE(BM_PowTemplated, float, double, long double);

https://github.com/google/benchmark/的自述文件说

代码语言:javascript
复制
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

代码语言:javascript
复制
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 >

代码语言:javascript
复制
// 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不能迭代多个变体并从一行中定义多个变体。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38023017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档