我如何标记t1和t2两次并获得C中的毫秒差值?C编程语言中的时间戳
回答
这会给你的时间,以秒+微秒
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
tv.tv_sec // seconds
tv.tv_usec // microseconds
使用@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;
}
如果你想找到经过的时间,这种方法将工作只要你不在开始和结束之间重新启动计算机。
在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中这样做。
标准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;
类型的精度是实现定义,即日期时间差异可能只返回完整的秒。
还意识到clock()和usleep()之间的交互。 usleep()暂停程序,clock()只测量程序运行的时间。
提到here
/*
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;
}
输出如果可能会更好使用的gettimeofday():时间戳:20110912130940 // 2011年09月12十三点09分40秒
而不是格式化数据,可以使用asctime(tm),它返回一个字符串。 – 2012-05-04 22:17:10
这个怎么解决呢?在我的搜索中我没有看到类似的东西。我试图避免分裂并使解决方案更简单。
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);
- 1. C语言编程IAR-时间戳转换为可读格式
- 2. 编程语言C
- 3. C编程语言
- 4. 我的编程语言C#
- 5. Hive查询语言时间戳
- 6. 前往编程语言 - 系统时间
- 7. RPC编程语言C
- 8. CGI C语言编程
- 9. C语言编程指南
- 10. Shell中的C编程语言
- 11. D编程语言中的C#事件
- 12. 由K&R编写的C语言编程语言示例CH1
- 13. C程序中的NTP时间戳
- 14. 系统编程语言和应用程序编程语言之间的区别
- 15. strcat()在c语言编程中
- 16. 在C++编程语言中绑定语言
- 17. 从汇编语言编写C程序
- 18. C#中的时间戳
- 19. C中的时间戳。
- 20. 编程语言
- 21. Go语言time.Parse()用于没有时区的时间戳
- 22. C语言中:在编译
- 23. 编程语言的原语
- 24. C语言和多线程编程
- 25. 用于c编程语言的线程的计时器
- 26. 同步线程C语言中的时间测量
- 27. 用Linux编程C语言的参数
- 28. 编写的编程语言
- 29. 时间戳的语法
- 30. 编译io编程语言时出错
单凭标准C无法做到这一点。您可能的解决方案将有所不同,Linux,Windows,手机,收款机,...,微波炉,... – pmg 2009-09-18 13:26:21
@Christoph:time()和clock()都可以返回-1(转换为适当的类型)来表示函数在该实现中不可用...和time()(如果可用)的分辨率为1秒或更差。我细读了标准版,但没有发现clock()函数的任何硬性限制 – pmg 2009-09-18 17:58:21
在Linux手册页中:“请注意,时间可以环绕。在CLOCKS_PER_SEC等于1000000的32位系统上,此函数将近似返回相同的值每72分钟一班。“为了获得CPU时间getrusage()更好,虽然时钟是ANSI C的一部分,但getrusage/gettimeofday()不是。 – user172818 2009-09-18 18:58:30