gettimeofday()
2011-03-18 11:19
369 查看
gettimeofday() -- 获取当前时间(保存在结构体timeval中)
#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 <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
相关文章推荐
- gettimeofday windows上的实现
- struct timeval和gettimeofday()
- linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒(转载)
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- 关于"implicit declaration of function 'gettimeofday' is invalid in c99"的解决
- 谈谈时间函数gettimeofday
- C语言的时间函数(1)gettimeofday,timeval,timezone
- time,gettimeofday,clock_gettime,_ftime
- linux下的定时或计时操作(gettimeofday等的用法)
- gettimeofday()
- Linux时间函数之gettimeofday()函数之使用方法
- gettimeofday获取微妙
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)【转】
- sched_clock()、current_kernel_time()、getnstimeofday()
- linux时间函数time和gettimeofday
- gettimeofday和xtime的定时精度
- Linux C语言 高精度时间函数:gettimeofday()
- gettimeofday()函数的使用方法:
- [IMX6DL]do_gettimeofday()的精度分析
- C语言获取微秒级的系统时间 gettimeofday timeval timeout