linux gettimeofday 头文件,linux下得时间函数time 与gettimeofday

两个都是glibc获取时间的函数,    gettimeofday支持返回微妙的精度, time返回秒的精度,  在性能上有差别吗?

基本上没有性能差别, 因为time其实就是把gettimeofday包装了一层. 但是测试过程中发现 time比gettimeofday性能好了一点点, 可能是time函数的接口形式简单吧, 堆栈处理的快.

Epoch是指定为1970年1月1日凌晨零点零分零秒,格林威治时间。目前大部份的UNIX系统都是用32位来记录时间,正值表示为1970以后,负值则表示1970年以前。下面看下time.h中的time函数,sys/time.h中的gettimeofday函数#include

32位linux下的使用gettimeofday取毫秒数。

函数:

long long __getmstime()

{

timeval tv;

gettimeofday(&tv, NULL);

return        tv.tv_sec /1000 + tv.tv_usec / 1000 / 1000;

}

#include , #include 。

time()提供了秒级的精确度

1、头文件

2、函数原型

time_t time(time_t * timer)

函数返回从TC1970-1-1 0:0:0开始到现在的秒数

用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。

#include

#include

int main(void)

{

time_t t;

t = time(NULL);

printf("The number of seconds since January 1, 1970 is %ld",t);

return 0;

}

gettimeofday()提供了微秒级的精确度

1、头文件

2、函数原型

int gettimeofday(struct timeval *tv, struct timezone *tz);

gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。

参数说明:

timeval结构定义为:

struct timeval

{

long tv_sec; /*秒*/

long tv_usec; /*微秒*/

};

clock_gettime( ) 提供了纳秒级的精确度

1、头文件

2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数

3、函数原型

int clock_gettime(clockid_t clk_id, struct timespect *tp);

参数说明:

clockid_t clk_id 用于指定计时时钟的类型,有以下4种:

CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变

CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响

CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间

CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间

struct timespect *tp用来存储当前的时间,其结构如下:

struct timespec

{

time_t tv_sec; /* seconds */

long tv_nsec; /* nanoseconds */

};

返回值。0成功,-1失败

#include

#include

int main()

{

struct timespec ts;

clock_gettime(CLOCK_REALTIME, &ts);

printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec);

clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一个参数一样

printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec);

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);

printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);

clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);

printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);

printf("/n%d/n", time(NULL));

return 0;

}

/proc/uptime里面的两个数字分别表示:

the uptime of the system (seconds), and the amount of time spent in idle process (seconds).

把第一个数读出来,那就是从系统启动至今的时间,单位是秒

glibc time函数的实现: (可以看出是把gettimeofday包装了一层)

代码取自: glibc-2.17/sysdeps/posix/time.c

/* Return the current time as a `time_t' and also put it in *T if T is

not NULL. Time is represented as seconds from Jan 1 00:00:00 1970. */

time_t

time (t)

time_t *t;

{

struct timeval tv;

time_t result;

if (__gettimeofday (&tv, (struct timezone *) NULL))

result = (time_t) -1;

else

result = (time_t) tv.tv_sec;

if (t != NULL)

*t = result;

return result;

}

time 与 gettimeofday 两个函数得到的都是从Epoch开始到当前的秒数(tt=tv.tv_sec),而gettimeofday 还能得到更精细的微秒级结果,即tv_sec*(10^6)+tv_usec为从Epoch开始到当前的微秒数。

内核时间

内核有两个重要的全局变量:

unsigned long jiffies;

timeval xtime ;

jiffies是记录着从电脑开机到现在总共的"时钟中断"的次数。

文件linux-2.6.24/kernel/timer.c

void do_timer(unsigned long ticks)

{

jiffies_64 += ticks;

update_times(ticks);

}

xtime 是从cmos电路或rtc芯片中取得的时间,一般是从某一历史时刻开始到现在的时间。 这个就是所谓的"墙上时钟walltimer",通过它可计算得出操作系统需要的日期时间,它的精确度是微秒。 xtime第一次赋值是在系统启动时调用timekeeping_init或time_init进行的 再调用read_persistent_clock进一步调用get_rtc_time得到的 PS:在/proc/uptime里面的两个数字分别表示:    the uptime of the system(seconds), and the amount of time spent in idle process(seconds).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值