C语言 如何以毫秒为单位获得C中经过的时间?(视窗)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17250932/
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 to get the time elapsed in C in milliseconds? (Windows)
提问by Kaer
I've searched in the Web but I've only found a way for do it, but in this way it returns in seconds instead of milliseconds.
我在网上搜索过,但我只找到了一种方法,但通过这种方式,它以秒而不是毫秒为单位返回。
My code is:
我的代码是:
#include <stdio.h>
#include <assert.h>
#include <time.h>
int main(void)
{
int solucion;
time_t start, stop;
clock_t ticks;
long count;
time(&start);
solucion = divisores_it(92000000, 2);
time(&stop);
printf("Finnished in %f seconds. \n", difftime(stop, start));
return 0;
}
回答by Angus Comber
A cross platform way is to use ftime.
跨平台的方式是使用 ftime。
Windows specific link here: http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx
Windows 特定链接:http: //msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx
Example below.
下面举例。
#include <stdio.h>
#include <sys\timeb.h>
int main()
{
struct timeb start, end;
int diff;
int i = 0;
ftime(&start);
while(i++ < 999) {
/* do something which takes some time */
printf(".");
}
ftime(&end);
diff = (int) (1000.0 * (end.time - start.time)
+ (end.millitm - start.millitm));
printf("\nOperation took %u milliseconds\n", diff);
return 0;
}
I ran the code above and traced through it using VS2008 and saw it actually calls the windows GetSystemTimeAsFileTime function.
我运行上面的代码并使用 VS2008 跟踪它,看到它实际上调用了 windows GetSystemTimeAsFileTime 函数。
Anyway, ftime will give you milliseconds precision.
无论如何, ftime 会给你毫秒精度。
回答by mcrisc
The solution below seems OK to me. What do you think?
下面的解决方案对我来说似乎没问题。你怎么认为?
#include <stdio.h>
#include <time.h>
long timediff(clock_t t1, clock_t t2) {
long elapsed;
elapsed = ((double)t2 - t1) / CLOCKS_PER_SEC * 1000;
return elapsed;
}
int main(void) {
clock_t t1, t2;
int i;
float x = 2.7182;
long elapsed;
t1 = clock();
for (i=0; i < 1000000; i++) {
x = x * 3.1415;
}
t2 = clock();
elapsed = timediff(t1, t2);
printf("elapsed: %ld ms\n", elapsed);
return 0;
}
Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html#clock
参考:http: //www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html#clock
回答by Imre Kerr
For Windows, GetSystemTime()is what you want. For POSIX, gettimeofday().
对于 Windows,GetSystemTime()正是您想要的。对于POSIX, gettimeofday()。
回答by alk
GetSystemTime()uses the structure SYSTEMTIME, which provides milli-second resolution.
GetSystemTime()使用SYSTEMTIME提供毫秒级分辨率的结构。
回答by techcraver
This code piece works. This is based on the answer from Angus Comber:
这段代码有效。这是基于 Angus Comber 的回答:
#include <sys/timeb.h>
uint64_t system_current_time_millis()
{
#if defined(_WIN32) || defined(_WIN64)
struct _timeb timebuffer;
_ftime(&timebuffer);
return (uint64_t)(((timebuffer.time * 1000) + timebuffer.millitm));
#else
struct timeb timebuffer;
ftime(&timebuffer);
return (uint64_t)(((timebuffer.time * 1000) + timebuffer.millitm));
#endif
}

