C语言 在 C 中测量时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13156031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measuring time in C
提问by aalf1987
I'm trying to measure some activity in C (Matrix multiplying) and noticed that I should do something like this:
我正在尝试测量 C(矩阵乘法)中的一些活动,并注意到我应该做这样的事情:
clock_t start = clock();
sleep(3);
clock_t end = clock();
double elapsed_time = (end - start)/(double)CLOCKS_PER_SEC;
printf("Elapsed time: %.2f.\n", elapsed_time);
The output is:
输出是:
Elapsed time: 0.00.
Why is this happening?
为什么会这样?
回答by Fred Foo
clockestimates the CPU time used by your program; that's the time the CPU has been busy executing instructions belonging to your program. sleepdoesn't perform any work, so it takes no noticeable CPU time (even if it takes wallclock time).
clock估计您的程序使用的 CPU 时间;那是 CPU 忙于执行属于您的程序的指令的时间。sleep不执行任何工作,因此不会占用明显的 CPU 时间(即使需要挂钟时间)。
If you want to measure wallclock time, use time:
如果要测量挂钟时间,请使用time:
time_t start = time(NULL);
sleep(3);
printf("%.2f\n", (double)(time(NULL) - start));
will print a number close to three.
将打印一个接近三的数字。
回答by Romuald Brunet
As a side note, if you want to measure execution time in a more precise manner (milliseconds), timeis not precise enough. You can use gettimeofdayinstead:
附带说明一下,如果您想以更精确的方式(毫秒)测量执行时间,time则不够精确。您可以gettimeofday改用:
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
int main() {
long start, end;
struct timeval timecheck;
gettimeofday(&timecheck, NULL);
start = (long)timecheck.tv_sec * 1000 + (long)timecheck.tv_usec / 1000;
usleep(200000); // 200ms
gettimeofday(&timecheck, NULL);
end = (long)timecheck.tv_sec * 1000 + (long)timecheck.tv_usec / 1000;
printf("%ld milliseconds elapsed\n", (end - start));
return 0;
}
回答by Aniket Inge
You must use time_t start = time(NULL);and time_t end = time(NULL);to get the correct values.
您必须使用time_t start = time(NULL);和time_t end = time(NULL);来获得正确的值。
回答by Orwell
If you don't care about being tied to Windows, you can try the high resolution timer. It's is a lot more precise than time(), which only has a precision of a single second because it the uses UNIX format.
如果你不在乎被绑定到 Windows,你可以尝试高分辨率计时器。它比 time() 精确得多,它只有一秒的精度,因为它使用 UNIX 格式。
#include <iostream>
#include <windows.h>
__int64 countspersec = 0;
double secpercount = 0.0;
__int64 starttime = 0;
__int64 curtime = 0;
int main() {
// Get current time, and determine how fast the clock ticks
QueryPerformanceCounter((LARGE_INTEGER*)&starttime);
QueryPerformanceFrequency((LARGE_INTEGER*)&countspersec);
secpercount = 1.0/(double)countspersec;
/* calculate something */
// Standard end-start stuff, account for clock speed
QueryPerformanceCounter((LARGE_INTEGER*)&curtime);
std::cout << "Time needed: " << (curtime-starttime)*secpercount << " sec\n";
return 0;
}
回答by Arno
Use QueryPerformanceFrequency()as described in Orwells answer or use the GetSystemTimeAsFileTime()function. The latter has 100 ns granularity but does not increment at that rate. Its increment depends on underlaying hardware and the setting of multimedia timer resolution.
Keep in mind that the frequency returned by QueryPerformanceFrequency()is treated as a constant. However, since it is generated by hardware it has an offset and a drift in time too. Measuring periods in time by using QueryPerformanceCounter()will typically be accompanied by errors of many microseconds per second.
I've given thisand thisanswer about similar matters.
使用Orwells answer 中所述的QueryPerformanceFrequency()或使用GetSystemTimeAsFileTime()函数。后者具有 100 ns 的粒度,但不会以该速率增加。它的增量取决于底层硬件和多媒体定时器分辨率的设置。请记住,返回的频率QueryPerformanceFrequency()被视为常数。但是,由于它是由硬件生成的,因此它也有偏移和时间漂移。使用测量时间段QueryPerformanceCounter()通常会伴随每秒许多微秒的误差。我已经给出了这个和这个关于类似问题的答案。

