linux中C语言获取高精度时钟gettimeofday函数
来源:互联网 发布:同花顺mac版好用吗 编辑:程序博客网 时间:2024/05/17 02:51
前言:
在开发中,很多时候需要知道各个函数或者是某些设备对命令的操作用时,因此需要用到 gettimeofday 来获取当前时钟。
一,函数说明
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
注意:
在开发中,很多时候需要知道各个函数或者是某些设备对命令的操作用时,因此需要用到 gettimeofday 来获取当前时钟。
一,函数说明
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
注意:
1.精确级别,微妙级别
2.受系统时间修改影响
3.返回的秒数是从1970年1月1日0时0分0秒开始
其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:
结构体timeval的定义为:
它获得的时间精确到微秒(1e-6 s)量级
结构体timezone的定义为:
timezone 参数若不使用则传入NULL即可。
其中 tz_dsttime 的值:
返回值
成功则返回0,失败返回-1,错误代码存于errno。
ERRORS
EFAULT One of tv or tz pointed outside the accessible address space.
EINVAL Timezone (or something else) is invalid.
二,实例
2.受系统时间修改影响
3.返回的秒数是从1970年1月1日0时0分0秒开始
其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果:
结构体timeval的定义为:
点击(此处)折叠或打开
struct timeval { long int tv_sec; // 秒数 long int tv_usec; // 微秒数 }
结构体timezone的定义为:
点击(此处)折叠或打开
struct timezone { int tz_minuteswest;/*格林威治时间往西方的时差*/ int tz_dsttime; /*DST 时间的修正方式*/ }
其中 tz_dsttime 的值:
点击(此处)折叠或打开
DST_NONE /*不使用*/ DST_USA /*美国*/ DST_AUST /*澳洲*/ DST_WET /*西欧*/ DST_MET /*中欧*/ DST_EET /*东欧*/ DST_CAN /*加拿大*/ DST_GB /*大不列颠*/ DST_RUM /*罗马尼亚*/ DST_TUR /*土耳其*/ DST_AUSTALT /*澳洲(1986年以后)*/
成功则返回0,失败返回-1,错误代码存于errno。
ERRORS
EFAULT One of tv or tz pointed outside the accessible address space.
EINVAL Timezone (or something else) is invalid.
二,实例
点击(此处)折叠或打开
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/time.h> 5 int time_substract(struct timeval *result, struct timeval *begin,struct timeval *end) 6 { 7 if(begin->tv_sec > end->tv_sec) return -1; 8 if((begin->tv_sec == end->tv_sec) && (begin->tv_usec > end->tv_usec)) return -2; 9 result->tv_sec = (end->tv_sec - begin->tv_sec);10 result->tv_usec = (end->tv_usec - begin->tv_usec);11 12 if(result->tv_usec < 0)13 {14 result->tv_sec--;15 result->tv_usec += 1000000;16 }17 return 0;18 }19 int main(int argc, char **argv)20 {21 struct timeval start,stop,diff;22 memset(&start,0,sizeof(struct timeval));23 memset(&stop,0,sizeof(struct timeval));24 memset(&diff,0,sizeof(struct timeval));25 gettimeofday(&start,0);26 //做你要做的事...27 printf("hello world\n");28 gettimeofday(&stop,0);29 time_substract(&diff,&start,&stop);30 printf("Total time : %d s,%d us\n",(int)diff.tv_sec,(int)diff.tv_usec);31 }
操作结果:
转载自:http://blog.chinaunix.net/uid-28458801-id-4214306.html
0 0
- linux中C语言获取高精度时钟gettimeofday函数
- linux C 高精度时间函数:gettimeofday()
- C语言gettimeofday()函数:获取当前时间
- C语言函数gettimeofday
- linux墙上时间xtime与高精度时钟gettimeofday
- 高精度gettimeofday()函数用法
- (笔记)linux c获取当前时间的函数gettimeofday(毫秒微秒)
- C语言linux环境下使用gettimeofday函数得到程序运行时间
- c函数之gettimeofday()函数获取系统时间
- 时间编程--gettimeofday(获取高精度时间)
- C语言的时间函数(1)gettimeofday,timeval,timezone
- linux中gettimeofday用法
- Linux 时间获取函数-C语言
- Linux时间获取函数-c语言
- Linux 系统函数记录 gettimeofday
- linux时间函数gettimeofday解析
- linux时间函数gettimeofday解析
- C语言中获取文件大小的函数
- 约瑟夫环问题
- C++STL之迭代器
- C++STL之迭代器2
- 支付宝接口开发
- 机器学习 之 贝叶斯分类(下)
- linux中C语言获取高精度时钟gettimeofday函数
- C++引用
- 缓冲区溢出问题
- C中不安全函数
- iOS XMPP 注册用户失败 error:403 解决办法
- extern关键字
- malloc calloc 和 realloc
- memcpy函数
- easyUI合并行的单元格