2009-09-18 66 views
22

我如何标记t1和t2两次并获得C中的毫秒差值?C编程语言中的时间戳

+5

单凭标准C无法做到这一点。您可能的解决方案将有所不同,Linux,Windows,手机,收款机,...,微波炉,... – pmg 2009-09-18 13:26:21

+0

@Christoph:time()和clock()都可以返回-1(转换为适当的类型)来表示函数在该实现中不可用...和time()(如果可用)的分辨率为1秒或更差。我细读了标准版,但没有发现clock()函数的任何硬性限制 – pmg 2009-09-18 17:58:21

+1

在Linux手册页中:“请注意,时间可以环绕。在CLOCKS_PER_SEC等于1000000的32位系统上,此函数将近似返回相同的值每72分钟一班。“为了获得CPU时间getrusage()更好,虽然时钟是ANSI C的一部分,但getrusage/gettimeofday()不是。 – user172818 2009-09-18 18:58:30

回答

28

这会给你的时间,以秒+微秒

#include <sys/time.h> 
struct timeval tv; 
gettimeofday(&tv,NULL); 
tv.tv_sec // seconds 
tv.tv_usec // microseconds 
0

U可以尝试在C时间库(time.h)例程。另外在同一个库中查看clock()。自编程开始以来,它会给出时钟滴答声。但是你可以在你想要专注的操作之前保存它的值,然后在操作之后再次捕获cliock ticks并找出差异,然后获得时间差。

3

使用@Arkaitz希门尼斯的代码,以获得两个timevals:

#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可以在this web site发现:

/* 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; 
} 
+0

timeval_subtract中的代码是邪恶的,因为它修改了输入值y。如果输入是两个结构timeval值 - 这与指针不同,那不会有什么不好。但是当评估'x - y'时,通常不会期望计算改变存储在'y'中的值。 – 2009-09-18 13:33:50

+0

@Jonathan,真的。虽然简单地将其更改为通过复制实施将解决该问题 – Glen 2009-09-18 13:43:14

+0

我同意。我会修复它,但我不能仔细检查我的修改是否会在此刻编译,所以我想我会保持原样。 – Bill 2009-09-18 13:55:43

6

如果你想找到经过的时间,这种方法将工作只要你不在开始和结束之间重新启动计算机。

在Windows中,使用GetTickCount()。具体方法如下:现在

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

dwElapsed是经过的毫秒数。

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

如果您需要持续重启或跨PC的时间戳(确实需要相当好的syncronization),然后使用其他方法(gettimeofday())。

另外,在Windows中,至少你可以比标准时间分辨率好得多。通常,如果您在紧密循环中调用GetTickCount(),则每次更改时都会看到它跳跃10-50。这是因为Windows线程调度程序使用的时间量。这或多或少是每个线程在切换到别的之前运行的时间量。在你的程序或进程和年初

timeBeginPeriod(1); 

:如果你做一个

timeEndPeriod(1); 

末,那么量子将变更为1毫秒,您将得到更好的时间分辨率GetTickCount()调用。但是,这确实会对整个计算机如何运行进程做出微妙的更改,因此请记住这一点。然而,Windows Media Player和其他许多事情无论如何都是这样做的,所以我不必太担心。我敢肯定,在Linux中可能有某种方法可以做到这一点(可能有更好的控制,或者可能有亚毫秒级的量子),但我还没有必要在Linux中这样做。

8

标准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; 

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

+0

日期时间差最多返回完整秒。如果我正确地解释了标准,则time()不返回(time_t)-1时,不能保证每秒都返回新值:例如,它可以具有5秒或1分钟的分辨率。 – pmg 2009-09-18 18:04:37

+2

@pmg:精度是实现定义的,例如在我的系统上'time()'具有'1s'分辨率; 'clock()'的精度通常尽可能高,但它可以测量运行时间而不是日期时间 – Christoph 2009-09-18 18:25:05

1

还意识到clock()和usleep()之间的交互。 usleep()暂停程序,clock()只测量程序运行的时间。

提到here

5
/* 
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; 

} 

输出如果可能会更好使用的gettimeofday():时间戳:20110912130940 // 2011年09月12十三点09分40秒

+0

而不是格式化数据,可以使用asctime(tm),它返回一个字符串。 – 2012-05-04 22:17:10

2

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

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);