在Windows上,有比clock()
和gettickcount()
函数更好的测量时间的方法吗? 当我尝试使用这些函数进行短操作时,它显示运行时间为0.0秒。 我需要一种以毫秒或纳秒为单位测量时间的方法。
在Windows上,有比clock()
和gettickcount()
函数更好的测量时间的方法吗? 当我尝试使用这些函数进行短操作时,它显示运行时间为0.0秒。 我需要一种以毫秒或纳秒为单位测量时间的方法。
你可以使用 QueryPerformanceCounter
和 QueryPerformanceFrequency
:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(void)
{
LARGE_INTEGER frequency;
LARGE_INTEGER start;
LARGE_INTEGER end;
double interval;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);
// code to be measured
QueryPerformanceCounter(&end);
interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;
printf("%f\n", interval);
return 0;
}
QueryPerformanceFrequency
的文档)。 - pcworldclock()
提供的是墙上时间而不是执行时间。我尝试使用QPC方法以获得一致的计时(用于分析),但遗憾的是测量时间也会有所变化。这适用于1秒或更长时间段,并且对于每次运行完全相同的操作,因此不能测量执行时间。因此,我仍然需要进行多次运行并记录最短时间。 - Weather Vane