How to evaluate a program's runtime?

后端 未结 1 1384
旧时难觅i
旧时难觅i 2021-01-05 19:56

I\'ve developed a simple program and want to evaluate its runtime performance on a real machine, e.g. my MacBook. The source code goes:

#include 

        
相关标签:
1条回答
  • 2021-01-05 20:23

    Benchmarking is hard!

    Short answer: use google benchmark

    Long answer: There are many things that will interfere with timings.

    • Scheduling (the OS running other things instead of you)
    • CPU Scaling (the OS deciding it can save energy by running slower)
    • Memory contention (Something else using the memory when you want to)
    • Bus contention (Something else talking to a device you want to talk to)
    • Cache (The CPU holding on to a value to avoid having to use memory)
    • CPU migration. (The OS moving you from one CPU to another)
    • Inaccurate clocks (Only CPU clocks are accurate to any degree, but they change if you migrate)

    The only way to avoid these effects are to disable CPU scaling, to do "cache-flush" functions (normally just touching a lot of memory before starting), running at high priority, and locking yourself to a single CPU. Even after all that, your timings will still be noisy, so the last thing is simply to repeat a lot, and use the average.
    This why tools like google benchmark are probably your best bet.

    video from CPPCon
    Also available live online

    0 讨论(0)
提交回复
热议问题