Quick and dirty way to profile your code

后端 未结 7 522
轮回少年
轮回少年 2020-12-04 18:25

What method do you use when you want to get performance data about specific code paths?

7条回答
  •  情歌与酒
    2020-12-04 18:59

    I wrote a simple cross-platform class called nanotimer for this reason. The goal was to be as lightweight as possible so as to not interfere with actual code performance by adding too many instructions and thereby influencing the instruction cache. It is capable of getting microsecond accuracy across windows, mac and linux (and probably some unix variants).

    Basic usage:

    plf::timer t;
    timer.start();
    
    // stuff
    
    double elapsed = t.get_elapsed_ns(); // Get nanoseconds
    

    start() also restarts the timer when necessary. "Pausing" the timer can be achieved by storing the elapsed time, then restarting the timer when "unpausing" and adding to the stored result the next time you check elapsed time.

提交回复
热议问题