struct timeval结构体 以及 gettimeofday()函数
来源:互联网 发布:java web菜单权限控制 编辑:程序博客网 时间:2024/05/20 07:16
原文地址http://blog.chinaunix.net/uid-20548989-id-2533161.html
一、struct timeval结构体
struct timeval结构体在time.h中的定义为:
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
- struct timeval
- {
- __time_t tv_sec; /* Seconds.*/
- __suseconds_t tv_usec; /* Microseconds.*/
- };
- #include <sys/time.h>
- #include <stdio.h>
-
- int
- main(void)
- {
- int i;
- struct timeval tv;
- for(i= 0; i < 4; i++){
- gettimeofday(&tv,NULL);
- printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
- sleep(1);
- }
- return 0;
- }
- 329612 1314851429
- 329782 1314851430
- 329911 1314851431
- 330036 1314851432
二、gettimeofday()函数
原型:
- /*Get the current time ofday and timezone information,
- putting it into *TV and *TZ.If TZ is NULL, *TZis not filled.
- Returns 0 on success,-1 on errors.
- NOTE: This form of timezone information is obsolete.
- Use the functions and variables declaredin <time.h> instead.*/
- extern int gettimeofday (struct timeval *__restrict __tv,
- __timezone_ptr_t __tz) __THROW __nonnull((1));
gettimeofday()功能是得到当前时间和时区,分别写到tv和tz中,如果tz为NULL则不向tz写入。
阅读全文
0 0
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- struct timeval结构体 以及 gettimeofday()函数
- 有关struct timeval结构体 以及 gettimeofday()函数
- 十七 struct timeval结构体 以及 gettimeofday()函数
- linux系统中struct timeval结构体、struct timezone结构体以及gettimeofday函数
- struct timeval结构体
- struct timeval结构体
- struct timeval结构体
- Volley源码解析(二)
- [特斯拉组件]ios高性能PageController
- ajax 轮询请求后台服务器
- HTML添加输入框,按钮。
- 查询SQLSERVER表结构脚本
- struct timeval结构体 以及 gettimeofday()函数
- 面试笔试整理7:SQL问题整理
- 设计模式-单一职责原则
- 第二章 HTTP报文
- 3.大概了解Tornado框架的设计模型
- 计算日期差的函数方法
- 微信小程序语音识别服务搭建全过程解析(内附免费的供小程序语音识别的https服务)
- 流水号生成
- 2017年教师资格证——《教育知识与能力》(中学)知识点(一)