如何在C语言中对两个时间戳t1和t2进行计算并返回以毫秒为单位的时间差?
如何在C语言中对两个时间戳t1和t2进行计算并返回以毫秒为单位的时间差?
这将给你秒和微秒的时间
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
tv.tv_sec // seconds
tv.tv_usec // microseconds
标准C99:
#include <time.h>
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;
类型的精度是实现定义的,即日期时间差可能只返回完整的秒数。
time()
具有1s
分辨率; clock()
的精度通常尽可能高,但它测量的是运行时间而不是日期时间。 - Christoph/*
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(<ime);
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
如果您想查找经过的时间,只要在开始和结束之间不重新启动计算机,此方法就可以使用。
在Windows中,使用GetTickCount()。以下是操作步骤:
DWORD dwStart = GetTickCount();
...
... process you want to measure elapsed time for
...
DWORD dwElapsed = GetTickCount() - dwStart;
dwElapsed现在是已经过去的毫秒数。
在Linux中,使用clock()和CLOCKS_PER_SEC可以实现类似的功能。
如果你需要跨重启或跨计算机的时间戳(这需要相当好的同步),那么请使用其他方法(如gettimeofday())。
此外,在Windows中,您可以获得比标准时间分辨率更高的精度。通常,如果您在一个紧密的循环中调用GetTickCount(),每次它改变时都会看到它跳动10-50。这是由Windows线程调度器使用的时间量决定的。这基本上是它在切换到其他内容之前给予每个线程运行的时间量。如果您执行以下操作:
timeBeginPeriod(1);
在您的程序或进程开始时使用:
timeEndPeriod(1);
使用@Arkaitz Jimenez的代码获取两个timeval:
#include <sys/time.h>
//...
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(),如此处所述。
#include <sys/time.h>
time_t tm = time(NULL);
char stime[4096];
ctime_r(&tm, stime);
stime[strlen(stime) - 1] = '\0';
printf("%s",stime);
这个程序清楚地展示了如何做到这一点。它需要1秒的时间暂停,然后再花费时间2,两个时间之间的差应该是1000毫秒。所以你的答案是正确的。
#include <stdio.h>
#include <time.h>
#include <unistd.h>
// Name: miliseconds.c
// gcc /tmp/miliseconds.c -o miliseconds
struct timespec ts1, ts2; // time1 and time2
int main (void) {
// get time1
clock_gettime(CLOCK_REALTIME, &ts1);
sleep(1); // 1 second pause
// get time2
clock_gettime(CLOCK_REALTIME, &ts2);
// nanoseconds difference in mili
long miliseconds1= (ts2.tv_nsec - ts1.tv_nsec) / 10000000 ;
// seconds difference in mili
long miliseconds2 = (ts2.tv_sec - ts1.tv_sec)*1000;
long miliseconds = miliseconds1 + miliseconds2;
printf("%ld\n", miliseconds);
return 0;
}