gettimeofday() -- 获取当前时间(保存在结构体timeval中)
来源:互联网 发布:2016年10月经济数据 编辑:程序博客网 时间:2024/06/05 16:03
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char * argv[]){
struct timeval tv; //(1)
while(1){
gettimeofday(&tv, NULL); //(2)
printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
sleep(2);
}
return 0;
}
(1) struct--timeval
--------------------------------------------------
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
millisecond 毫秒
microsecond 微秒
timeval表示一个时间点,比如:
timeval.tv_sec = 1 (s)
timevat.tv_usec = 500 000 (μs)
1:500 = 1s500000μs = 1.5s
(2) gettimeofday()
--------------------------------------------------
int gettimeofday(struct timeval *tv, struct timezone *tz);
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.
(3) 运行结果:
--------------------------------------------------
time 1181788367:991487
time 1181788369:991602
表示睡眠2秒经过的精确时间为: 2s115μs
#include <sys/time.h>
#include <time.h>
int main(int argc, char * argv[]){
struct timeval tv; //(1)
while(1){
gettimeofday(&tv, NULL); //(2)
printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
sleep(2);
}
return 0;
}
(1) struct--timeval
--------------------------------------------------
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
millisecond 毫秒
microsecond 微秒
timeval表示一个时间点,比如:
timeval.tv_sec = 1 (s)
timevat.tv_usec = 500 000 (μs)
1:500 = 1s500000μs = 1.5s
(2) gettimeofday()
--------------------------------------------------
int gettimeofday(struct timeval *tv, struct timezone *tz);
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.
(3) 运行结果:
--------------------------------------------------
time 1181788367:991487
time 1181788369:991602
表示睡眠2秒经过的精确时间为: 2s115μs
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)【转】
- gettimeofday() -- 获取当前时间
- 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()函数
- 多线程的计时器
- 项目学习 [ java分布式通信 ]
- Hdu 2295 (二分+重复覆盖问题 Dancing Links)
- 带你走进缓存世界(3):缓存原理
- Yii framework 学习总结中~可不看~
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- OSI的七层结构
- 心里总想发泄一下……诶
- java修改session超时时间
- 安装 IBM Full-System Simulator:PowerPC 970 system (1)
- Hibernate主键生成方式
- 带你走进缓存世界(4):缓存之缓
- XP 及Linux 增删 路由与网段
- OSI模型与TCP/IP模型的比较