Google基准测试自定​​义主程序 [英] Google benchmark custom main

查看:134
本文介绍了Google基准测试自定​​义主程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基准测试开始与Google的基准测试库一起运行之前,我想调用一个自定义的main函数.这样我可以设置几件事.我已经搜索了很多,但找不到任何东西.我应该简单地手动修改宏吗?或者只是使用我的主要功能并自己初始化基准.这会以任何方式影响库的初始化吗?是否有另一种方法不需要我修改该宏或复制其内容?

I would like to have a custom main function called before the benchmark starts to run with Google's benchmark library. So that I could setup several things. I've searched for quite a bit but I wasn't able to find anything. Should I simply modify the macro manually? Or simply use my main function and initialize the benchmark myself. Would that affect the library initialization in any way? Is there another way without requiring me to modify that macro or copying it's contents?

benchmark \ benchmark_api.h

// Helper macro to create a main routine in a test that runs the benchmarks
#define BENCHMARK_MAIN()                   \
  int main(int argc, char** argv) {        \
    ::benchmark::Initialize(&argc, argv);  \
    ::benchmark::RunSpecifiedBenchmarks(); \
  }

推荐答案

BENCHMARK_MAIN()只是一个辅助宏,因此您应该能够定义自己的 main()版本像这样:

BENCHMARK_MAIN() is just a helper macro, so you should be able to define your own version of main() like this:

int main(int argc, char** argv)
{
   your_custom_init();
   ::benchmark::Initialize(&argc, argv);
   ::benchmark::RunSpecifiedBenchmarks();
}

编辑:您还可以定义全局对象并在其构造函数中执行自定义初始化.我通常以这种方式进行操作,例如用输入数据初始化全局数组:

you can also define global object and perform your custom initialization within its constructor. I usually do it this way, e.g. to initialize global array with input data:

int data[10];

class MyInit
{
public:
    MyInit()
    {
        for (int n = 0; n < 10; ++n)
            data[n] = n;
    }
};

MyInit my_init;

这篇关于Google基准测试自定​​义主程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆