precise time measurement

前端 未结 4 2012
甜味超标
甜味超标 2020-12-07 11:58

I\'m using time.h in C++ to measure the timing of a function.

clock_t t = clock();
someFunction();
printf(\"\\nTime taken: %.4fs\\n\", (float)(clock() - t)/         


        
4条回答
  •  盖世英雄少女心
    2020-12-07 12:11

    The following text, that i completely agree with, is quoted from Optimizing software in C++ (good reading for any C++ programmer) -

    The time measurements may require a very high resolution if time intervals are short. In Windows, you can use the GetTickCount or QueryPerformanceCounter functions for millisecond resolution. A much higher resolution can be obtained with the time stamp counter in the CPU, which counts at the CPU clock frequency.

    There is a problem that "the clock frequency may vary dynamically and that measurements are unstable due to interrupts and task switches."

提交回复
热议问题