struct timeval结构体
struct timeval结构体在time.h中的定义为:
struct timeval{__time_t tv_sec; /* Seconds. */__suseconds_t tv_usec; /* Microseconds. */ //微妙,us, 1s=1000ms=1000000us };
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
#include <sys/time.h>#include <stdio.h>intmain(void){int i;struct timeval tv;for(i = 0; i < 4; i++){gettimeofday(&tv, NULL);printf("%d.%d\n", tv.tv_sec, tv.tv_usec);sleep(1);}return 0;}
前面为秒数,小数点后面为微秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
gettimeofday()函数
原型:
/* Get the current time of day and timezone information,putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.Returns 0 on success, -1 on errors.NOTE: This form of timezone information is obsolete.Use the functions and variables declared in <time.h> instead. */extern int gettimeofday (struct timeval *__restrict __tv,__timezone_ptr_t __tz) __THROW __nonnull ((1));
gettimeofday()功能是得到当前时间和时区,分别写到tv和tz中,如果tz为NULL则不向tz写入。TZ是时区,time zone ,TV是时间值,即time value。
struct timespec
typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec)获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tmlocaltime(const time_tclock); //线程不安全
struct tmlocaltime_r( const time_ttimer, struct tmresult );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
#include<stdio.h>#include<time.h>#include<sys/time.h>void nowtime_ns(){printf("---------------------------struct timespec---------------------------------------\n"); printf("[time(NULL)]:%ld\n", time(NULL)); struct timespec ts;clock_gettime(CLOCK_REALTIME, &ts);printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec);struct tm t;char date_time[64];strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));printf("clock_gettime : date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);}void nowtime_us(){printf("---------------------------struct timeval----------------------------------------\n"); printf("[time(NULL)] : %ld\n", time(NULL)); struct timeval us;gettimeofday(&us,NULL);printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);struct tm t;char date_time[64];strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);}int main(int argc, char* argv[]){nowtime_ns();printf("\n");nowtime_us();printf("\n");return 0;}
运行的结果:
---------------------------struct timespec---------------------------------------
[time(NULL)] : 1400233995
clock_gettime : tv_sec=1400233995, tv_nsec=828222000
clock_gettime : date_time=-05-16 17:53:15, tv_nsec=828222000
---------------------------struct timeval----------------------------------------
[time(NULL)] : 1400233995
gettimeofday: tv_sec=1400233995, tv_usec=828342
gettimeofday: date_time=-05-16 17:53:15, tv_usec=828342
参考:/uid-20548989-id-2533161.html
/book-gary/p/3716790.html
如果觉得《struct timeval结构体 以及 gettimeofday()函数 struct timespec结构体》对你有帮助,请点赞、收藏,并留下你的观点哦!