Google benchmark custom main

孤街浪徒 提交于 2021-02-07 14:49:45

问题


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(); \
  }

回答1:


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();
}

Edit: 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;


来源:https://stackoverflow.com/questions/34401965/google-benchmark-custom-main

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!