// 返回自系统开机以来的毫秒数(tick)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}
int main()
{
struct timespec time1 = { 0, 0 };
clock_gettime(CLOCK_REALTIME, &time1);
printf("CLOCK_REALTIME: %d, %d\n", time1.tv_sec, time1.tv_nsec);
clock_gettime(CLOCK_MONOTONIC, &time1);
printf("CLOCK_MONOTONIC: %d, %d\n", time1.tv_sec, time1.tv_nsec);
clock_gettime(CLOCK_MONOTONIC_RAW, &time1);
printf("CLOCK_MONOTONIC_RAW: %d, %d\n", time1.tv_sec, time1.tv_nsec);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d\n", time1.tv_sec,
time1.tv_nsec);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
printf("CLOCK_THREAD_CPUTIME_ID: %d, %d\n", time1.tv_sec,
time1.tv_nsec);
printf("\n%d\n", time(NULL));
printf("tick count in ms: %ul\n", GetTickCount());
return 0;
}
(原文:http://blog.csdn.net/guang11cheng/article/details/6865992)
本文介绍了一种在C/C++中获取系统时间的方法,并通过示例代码展示了如何使用clock_gettime函数来获取不同类型的系统时间,包括实时时间、单调递增时间等。
1270

被折叠的 条评论
为什么被折叠?



