2016-06-24 11 views
0

floatdoublelong doubleでgoogle /ベンチマークを実行します。google/benchmark: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(); 

は、私は、これはその後、フロートのための3つのベンチマークを作成することを想像し、ダブル、ダブル長いが、その代わりに、それはコンパイルされません:

BENCHMARK_TEMPLATEexampleを考えると、私は次のことを試みました!

テンプレート付きGoogleベンチマークを作成するための適切な構文は何ですか? BENCHMARK_TEMPLATEが正しく動作するかどうか、どうすればこのコードを修正できますか?

答えて

1

あなたはと間違った方法でBENCHMARK_TEMPLATEを使用し、あなたの

BENCHMARK_TEMPLATE(BM_PowTemplated, float, double, long double); 

https://github.com/google/benchmark/のreadmeファイルには、

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) 

だから、ARG1とBENCHMARK_TEMPLATEはARG1とARG2と、1つのテンプレートパラメータを持つ関数のために使用されていると言います2つのテンプレートパラメータを持つ関数の場合BM_PowTemplatedには1つのパラメータしかないので、BENCHMARK_TEMPLATEに3つの引数を使用することはできません。例えばグーグル/ベンチマークの

チェックtest/cxx03_test.cchttps://github.com/google/benchmark/blob/b2e734087532897b7bb4c51a6b4f503060c9a20f/test/cxx03_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:マクロの定義: https://github.com/google/benchmark/blob/2d088a9f2d41acb77afc99d045f669e1a21b61ef/include/benchmark/benchmark_api.h#L684

我々はaa, 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は複数のバリアントを反復処理することはできず、1行からいくつかのバリアントを定義することはできません。