C语言 我如何在C中测量时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3557221/
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
How do I measure time in C?
提问by snakile
I want to find out for how long (approximately) some block of code executes. Something like this:
我想知道某个代码块执行了多长时间(大约)。像这样的东西:
startStopwatch();
// do some calculations
stopStopwatch();
printf("%lf", timeMesuredInSeconds);
How?
如何?
回答by KLee1
回答by Stephen
You can use the time.hlibrary, specifically the timeand difftimefunctions:
您可以使用time.h库,特别是time和difftime函数:
/* difftime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t start,end;
double dif;
time (&start);
// Do some calculation.
time (&end);
dif = difftime (end,start);
printf ("Your calculations took %.2lf seconds to run.\n", dif );
return 0;
}
(Example adapted from the difftime webpage linked above.)
(示例改编自上面链接的 difftime 网页。)
Please note that this method can only give seconds worth of accuracy - time_trecords the seconds since the UNIX epoch(Jan 1st, 1970).
请注意,此方法只能提供几秒钟的准确度 -time_t记录自UNIX 时代(1970 年 1 月 1 日)以来的秒数。
回答by selbie
GetTickCount().
获取TickCount()。
#include <windows.h>
void MeasureIt()
{
DWORD dwStartTime = GetTickCount();
DWORD dwElapsed;
DoSomethingThatYouWantToTime();
dwElapsed = GetTickCount() - dwStartTime;
printf("It took %d.%3d seconds to complete\n", dwElapsed/1000, dwElapsed - dwElapsed/1000);
}
回答by Ivan Talalaev
Sometime it's needed to measure astronomical timerather than CPU time(especially this applicable on Linux):
有时需要测量天文时间而不是CPU 时间(尤其适用于Linux):
#include <time.h>
double what_time_is_it()
{
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
return now.tv_sec + now.tv_nsec*1e-9;
}
int main() {
double time = what_time_is_it();
printf("time taken %.6lf\n", what_time_is_it() - time);
return 0;
}
回答by Andreas Rejbrand
I would use the QueryPerformanceCounterand QueryPerformanceFrequencyfunctions of the Windows API. Call the former before and after the block and subtract (current − old) to get the number of "ticks" between the instances. Divide this by the value obtained by the latter function to get the duration in seconds.
我会使用Windows API的QueryPerformanceCounter和QueryPerformanceFrequency函数。在块之前和之后调用前者并减去(当前 - 旧)以获得实例之间的“滴答”数。将其除以后一个函数获得的值以获得以秒为单位的持续时间。
回答by user357501
If you don't need fantastic resolution, you could use GetTickCount(): http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx(If it's for something other than your own simple diagnostics, then note that this number can wrap around, so you'll need to handle that with a little arithmetic).
如果您不需要出色的分辨率,则可以使用 GetTickCount():http: //msdn.microsoft.com/en-us/library/ms724408( VS.85) .aspx(如果不是为了您自己的简单诊断,然后注意这个数字可以环绕,所以你需要用一点算术来处理它)。
QueryPerformanceCounter is another reasonable option. (It's also described on MSDN)
QueryPerformanceCounter 是另一个合理的选择。(MSDN 上也有描述)

