当前位置:网站首页>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秒 时钟数。
边栏推荐
- 【重温经典C语言】~c语言中%x、%c、%d、%x等等等、c语言取地址符&的作用、C语言中的 联合体
- 如何使用插件化机制优雅的封装你的请求hook
- 一瓶水引发的“战争”
- Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
- Speech signal processing - Fundamentals (V): Fourier transform
- 基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
- 建立自己的网站(13)
- 用于生成学习任务的量子神经网络2022最新综述
- Win10 R package installation error: not installed in arch=i386
- What is erdma as illustrated by Coptic cartoon?
猜你喜欢

“\“id\“ contains an invalid value“

EMC surge

21、wpf之绑定使用小记

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

论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》

一个悄然崛起的国产软件,低调又强大!

治数如治水,数据治理和数据创新难在哪?

HMS core audio editing service 3D audio technology helps create an immersive auditory feast

Review the writing software with characteristics

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning. Mapu and Beiyou proposed category semantic embedding rich in visual information
随机推荐
治数如治水,数据治理和数据创新难在哪?
R language de duplication operation unique duplicate filter
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ x_ The log10 function configures the value range of the X axis to be logarithmic coordinates
一瓶水引发的“战争”
Lucene全文检索工具包学习笔记总结
谁还记得「张同学」?
He was the first hero of Shanghai's two major industries, but died silently in regret
MySQL 内置函数
来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
Re understand oauth2.0 protocol for joint login
Learn how to implement distributed locks in redis - my own understanding
led背光板的作用是什么呢?
Customize an annotation to get a link to the database
WebView, Scrollview sliding conflict correction
A theoretical defect of relative position coding transformer and Its Countermeasures
1175. 质数排列
Another miserable day by kotlin grammar
Speech recognition - Fundamentals (I): introduction [speech to text]
这些电影中的科幻构想,已经用AI实现了
If it is not listed again, Kuangshi technology will not be able to endure