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

后端 未结 8 2005
忘了有多久
忘了有多久 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 03:00

    I would suppose that anyone competent enough to answer all your questions is gong to be far too busy to answer all your questions. In practice it is probably more effective to ask a single, well-defined questions. That way you may hope to get well-defined answers which you can collect and be on your way to wisdom.

    So, anyway, perhaps I can answer your question about which clock to use on Windows.

    clock() is not considered a high precision clock. If you look at the value of CLOCKS_PER_SEC you will see it has a resolution of 1 millisecond. This is only adequate if you are timing very long routines, or a loop with 10000's of iterations. As you point out, if you try and repeat a simple method 10000's of times in order to get a time that can be measured with clock() the compiler is liable to step in and optimize the whole thing away.

    So, really, the only clock to use is QueryPerformanceCounter()

提交回复
热议问题