当前位置:网站首页>time 函数和 clock_gettime()函数的区别
time 函数和 clock_gettime()函数的区别
2022-06-30 11:38:00 【仗剑天涯 回首枉然】
在Linux中我们有好多读时间的函数 , 但有的函数取得时间是绝对时间 当我们系统时间改变时 计算时间差会有影响。 下面我们就time() 和clock_gettime函数来说一下
函数"clock_gettime"是基于Linux C语言的时间函数,可以用于计算时间,有秒和纳秒两种精度。
函数原型:
int clock_gettime(clockid_t clk_id, struct timespec *tp);
其中,cld_id类型四种:
a、CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变
b、CLOCK_MONOTONIC,从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
c、CLOCK_PROCESS_CPUTIME_ID,本进程到当前代码系统CPU花费的时间
d、CLOCK_THREAD_CPUTIME_ID,本线程到当前代码系统CPU花费的时间
本文默认采用CLOCK_REALTIME,即可实现并行程序的准确计时。
其中,timespec结构包括:
struct timespec {
time_t tv_sec; /* 秒*/
long tv_nsec; /* 纳秒*/
};
而time()
首先看函数声明,该函数包含在time.h头文件中,其返回值类型为time_t。是自Epoch以来经过的秒数。如果其传入参数不为NULL,那么该秒数还会被赋给传入的参数中。
#include <time.h>
time_t time(time_t *timep);
// returns number of seconds since the Epoch, or -1 on error
times()函数
首先是times函数,声明如下。可以看到times函数的返回类型为clock_t,且其定义包含在头文件sys/time.h中。可以调用sysconf(_SC_CLK_TCK)来获得实际的时间。这里要注意times函数和上面提到的time函数的功能是不同的,times函数是获得进程时间的方法,time函数是获得时钟时间的方法。
原型如下:
clock_t times(struct tms *buf);
tms结构体如下:
strace tms{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
注释:
tms_utime记录的是进程执行用户代码的时间.
tms_stime记录的是进程执行内核代码的时间.
tms_cutime记录的是子进程执行用户代码的时间.
tms_cstime记录的是子进程执行内核代码的时
如果我们想计算 各个时间 就需要用到 sysconf(_SC_CLK_TCK)) 函数, 这个sysconf 函数可以去系统的信息。 _SC_CLK_TCK 去的是1秒 时钟数。
边栏推荐
- HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
- c# 怎样能写个sql的解析器
- [cf] 803 div2 A. XOR Mixup
- "New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- Database cascading operation
- Lucene full text search toolkit learning notes summary
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_x_log10函数配置X轴的数值范围为对数坐标
- The operation and maintenance security gateway (Fortress machine) of Qiming star group once again won the first place!
- WebView, Scrollview sliding conflict correction
猜你喜欢

MySQL 表的内连和外连

爱可可AI前沿推介(6.30)

AutoCAD - len command

服务器常用的一些硬件信息(不断更新)

In depth analysis of Apache bookkeeper series: Part 4 - back pressure

又被 Kotlin 语法糖坑惨的一天

wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD

聊聊怎么做硬件兼容性检测,快速迁移到openEuler?

STM32F407ZGT6使用SDIO方式驱动SD卡

对象映射 - Mapping.Mapster
随机推荐
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
数据库 事务
服务器常用的一些硬件信息(不断更新)
R language ggplot2 visual Facet: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF) and uses the labs function to add a dynamic time title to t
盘点那些具有特色的写作软件
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
Goto statement jump uninitialized variable: c2362
How to analyze native crash through GDB
TypeScript ReadonlyArray(只读数组类型) 详细介绍
EMC surge
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置show_legend参数为FALSE配置不显示图例信息
谁还记得「张同学」?
60 divine vs Code plug-ins!!
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
Learn how to implement distributed locks in redis - my own understanding
限时预约|6 月 Apache Pulsar 中文开发者与用户组会议
Typescript readonlyarray (read only array type) details
【云原生 | Kubernetes篇】深入了解Deployment(八)