Timing in an elegant way in c++

后端 未结 4 490
难免孤独
难免孤独 2021-01-03 01:31

I am interested in timing the execution time of a free function or a member function (template or not). Call TheFunc the function in question, its call being



        
4条回答
  •  鱼传尺愫
    2021-01-03 01:53

    You can do it the MatLab way. It's very old-school but simple is often good:

        tic();
        a = f(c);
        toc(); //print to stdout, or
        auto elapsed = toc(); //store in variable
    

    tic() and toc() can work to a global variable. If that's not sufficient, you can create local variables with some macro-magic:

        tic(A);
        a = f(c);
        toc(A);
    

提交回复
热议问题