C编程语言中的时间戳

如何标记t1和t2两次并获得C中的毫秒差异?

这将为您提供以秒为单位的时间+微秒

#include  struct timeval tv; gettimeofday(&tv,NULL); tv.tv_sec // seconds tv.tv_usec // microseconds 

标准C99:

 #include  time_t t0 = time(0); // ... time_t t1 = time(0); double datetime_diff_ms = difftime(t1, t0) * 1000.; clock_t c0 = clock(); // ... clock_t c1 = clock(); double runtime_diff_ms = (c1 - c0) * 1000. / CLOCKS_PER_SEC; 

类型的精度是实现定义的,即日期时间差异可能仅返回整秒。

如果要查找已用时间,只要不在开始和结束之间重新启动计算机,此方法就会起作用。

在Windows中,使用GetTickCount()。 这是如何做:

 DWORD dwStart = GetTickCount(); ... ... process you want to measure elapsed time for ... DWORD dwElapsed = GetTickCount() - dwStart; 

dwElapsed现在是经过的毫秒数。

在Linux中,使用clock()CLOCKS_PER_SEC来做同样的事情。

如果您需要通过重新启动或跨PC(这确实需要非常好的同步)的时间戳,那么使用其他方法(gettimeofday())。

此外,在Windows中,至少可以比标准时间分辨率更好。 通常情况下,如果你在一个紧凑的循环中调用GetTickCount(),你会发现它每次改变时都会跳跃10-50。 这是因为Windows线程调度程序使用的时间量。 这或多或少是在切换到其他内容之前为每个线程运行的时间量。 如果你这样做:

 timeBeginPeriod(1); 

在您的计划或流程的开头和:

 timeEndPeriod(1); 

最后,量子将变为1毫秒,您将在GetTickCount()调用上获得更好的时间分辨率。 但是,这确实会对整个计算机的运行过程进行微妙的更改,因此请记住这一点。 但是,无论如何,Windows Media Player和许多其他事情都是这样做的,所以我不担心它。

我确信在Linux中可能有一些方法可以做同样的事情(可能有更好的控制,或者可能有亚毫秒量子),但我还不需要在Linux中这样做。

 /* Returns the current time. */ char *time_stamp(){ char *timestamp = (char *)malloc(sizeof(char) * 16); time_t ltime; ltime=time(NULL); struct tm *tm; tm=localtime(&ltime); sprintf(timestamp,"%04d%02d%02d%02d%02d%02d", tm->tm_year+1900, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); return timestamp; } int main(){ printf(" Timestamp: %s\n",time_stamp()); return 0; } 

输出:时间戳:20110912130940 // 2011年9月12日13:09:40

使用@Arkaitz Jimenez的代码获得两个时间段:

 #include  //... struct timeval tv1, tv2, diff; // get the first time: gettimeofday(&tv1, NULL); // do whatever it is you want to time // ... // get the second time: gettimeofday(&tv2, NULL); // get the difference: int result = timeval_subtract(&diff, &tv1, &tv2); // the difference is storid in diff now. 

可以在以下网站找到timeval_subtract的示例代码:

  /* Subtract the `struct timeval' values X and Y, storing the result in RESULT. Return 1 if the difference is negative, otherwise 0. */ int timeval_subtract (result, x, y) struct timeval *result, *x, *y; { /* Perform the carry for the later subtraction by updating y. */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* Compute the time remaining to wait. tv_usec is certainly positive. */ result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; } 

这个解决方案怎么样? 我在搜索中没有看到这样的东西。 我试图避免分裂并使解决方案更简单。

  struct timeval cur_time1, cur_time2, tdiff; gettimeofday(&cur_time1,NULL); sleep(1); gettimeofday(&cur_time2,NULL); tdiff.tv_sec = cur_time2.tv_sec - cur_time1.tv_sec; tdiff.tv_usec = cur_time2.tv_usec + (1000000 - cur_time1.tv_usec); while(tdiff.tv_usec > 1000000) { tdiff.tv_sec++; tdiff.tv_usec -= 1000000; printf("updated tdiff tv_sec:%ld tv_usec:%ld\n",tdiff.tv_sec, tdiff.tv_usec); } printf("end tdiff tv_sec:%ld tv_usec:%ld\n",tdiff.tv_sec, tdiff.tv_usec); 

还要了解clock()和usleep()之间的交互。 usleep()挂起程序,clock()只测量程序运行的时间。

如果可能最好使用这里提到的gettimeofday()

使用gettimeofday()或更好的clock_gettime()

你可以在c时间库( time.h )中尝试例程。 另外看看同一个lib中的clock() 。 它提供了自编程开始以来的时钟滴答。 但是你可以在你想要专注的操作之前保存它的值,然后在那个操作之后再次捕获cliock,然后找到它们之间的差异以获得时差。