当前位置:网站首页>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秒 时钟数。
边栏推荐
- A quietly rising domestic software, low-key and powerful!
- wallys/3 × 3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
- 用于生成学习任务的量子神经网络2022最新综述
- Lucene全文检索工具包学习笔记总结
- 数据库 自动增长
- 21、wpf之绑定使用小记
- “\“id\“ contains an invalid value“
- Alibaba cloud database represented by polardb ranks first in the world
- 60 divine vs Code plug-ins!!
- R language ggplot2 visualization: use ggplot2 visualization scatter diagram and the size parameter in AES function to specify the size of data points (point size)
猜你喜欢
Redis - SDS simple dynamic string

Pointdistiller: structured knowledge distillation for efficient and compact 3D detection

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

The sci-fi ideas in these movies have been realized by AI

揭秘得物客服IM全链路通信过程

"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform

60 divine vs Code plug-ins!!

如何使用插件化机制优雅的封装你的请求hook

AUTOCAD——LEN命令

Is the golden cycle of domestic databases coming?
随机推荐
Alibaba cloud lifeifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
他是上海两大产业的第一功臣,却在遗憾中默默离世
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
koa - 洋葱模型浅析
Lucene full text search toolkit learning notes summary
A quietly rising domestic software, low-key and powerful!
If it is not listed again, Kuangshi technology will not be able to endure
[revisiting the classic C language] ~x,%c,%d,%x, etc. in C language, the role of the address character in C language, and the consortium in C language
Customize an annotation to get a link to the database
这些电影中的科幻构想,已经用AI实现了
对象映射 - Mapping.Mapster
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
1175. prime permutation
Constructor, class member, destructor call order
如何使用插件化机制优雅的封装你的请求hook
线下门店为什么要做新零售?
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute