Best way to test code speed in C++ without profiler, or does it not make sense to try?

后端 未结 8 2007
忘了有多久
忘了有多久 2021-02-06 02:01

On SO, there are quite a few questions about performance profiling, but I don\'t seem to find the whole picture. There are quite a few issues involved and most Q & A ignore

8条回答
  •  梦如初夏
    2021-02-06 02:43

    Obviously we would like to measure the cpu time of our code and not the real time, but as far as I understand, these functions don't give that functionality, so other processes on the system would interfere with measurements.

    I do two things, to ensure that wall-clock time and CPU time are approximately the same thing:

    • Test for a significant length of time, i.e. several seconds (e.g. by testing a loop of however many thousands of iterations)

    • Test when the machine is more or less relatively idle except for whatever I'm testing.

    Alternatively if you want to measure only/more exactly the CPU time per thread, that's available as a performance counter (see e.g. perfmon.exe).

    What can we know for certain without debugging, dissassembling and profiling tools?

    Nearly nothing (except that I/O tends to be relatively slow).

提交回复
热议问题