#include <stdio.h>
#include <sys/time.h>
#include <time.h>
struct timeval tvpre, tvafter;
gettimeofday(&tvpre, NULL);
sleep(3); //这里是linux编程,这个sleep是秒为单位的
gettimeofday(&tvafter, NULL);
ast_log(LOG_NOTICE,"prepaid_staff_log: test spend time is %d msecond./n", (tvafter.tv_sec-tvpre.tv_sec)*1000+(tvafter.tv_usec-tvpre.tv_usec)/1000);
输出是3002毫秒
这里要知道的是LINUX下sleep函数的参数是以秒为单位的,而wimdows下的是以毫秒为单位
还要注意一点是linux下的sleep函数是不精确的,当你需要非常精确时别依赖它
本文通过一个简单的C程序示例介绍了如何在Linux环境下使用gettimeofday函数来精确测量时间间隔,并强调了Linux与Windows环境下sleep函数的不同及Linux中该函数的非精确性。
4123

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



