Google Benchmark,如何仅调用一次代码?

问题描述 投票:0回答:1

我有一个代码段,我需要对基准测试进行2个部分的测试,首先要精确设置一次状态,接下来我需要对一个函数进行实际基准测试。

我的代码如下:

static void BM_LoopTime(benchmark::State& state) 
{
    MasterEngine engine;
    for (auto _ : state)
    {
        engine.LoopOnce();
    }
}
BENCHMARK(BM_LoopTime);

在我的输出中,我得到:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Pointer already set                                                             
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

多次,这是一个自定义错误消息,它指示一个非常重要的指针,试图覆盖该指针时应仅触摸一次。

多次调用该代码是我的实现中未定义的行为。我怎样才能只一次调用对象初始化,然后告诉它调用循环?

c++ performance benchmarking google-benchmark
1个回答
0
投票

这是一个变通的解决方案,我发现它足以满足我的用例,但我仍在寻找更好的解决方案:

class MyFixture : public benchmark::Fixture 
{
public:
    std::unique_ptr<MasterEngine> engine;

    void SetUp(const ::benchmark::State& state) 
    {
        if(engine.get() == nullptr)
            engine = std::make_unique<MasterEngine>();
    }

    void TearDown(const ::benchmark::State& state) 
    {
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.