当前位置:网站首页>Time function and clock_ Differences between gettime() functions
Time function and clock_ Differences between gettime() functions
2022-06-30 12:06:00 【Look back in vain】
stay Linux We have many functions of reading time in , But some functions get absolute time When our system time changes Calculating the time difference will have an impact . Now we're going to time() and clock_gettime Function
function "clock_gettime" Is based on Linux C Time function of language , Can be used to calculate time , There are two kinds of precision, second and nanosecond .
The function prototype :
int clock_gettime(clockid_t clk_id, struct timespec *tp);
among ,cld_id Four types :
a、CLOCK_REALTIME: System real time , It changes with the real time of the system
b、CLOCK_MONOTONIC, Start timing from the moment the system starts , Not affected by the change of system time by users
c、CLOCK_PROCESS_CPUTIME_ID, This process to the current code system CPU Time spent
d、CLOCK_THREAD_CPUTIME_ID, This thread to the current code system CPU Time spent
This article uses... By default CLOCK_REALTIME, The accurate timing of parallel programs can be realized .
among ,timespec The structure includes :
struct timespec {
time_t tv_sec; /* second */
long tv_nsec; /* nanosecond */
};
and time()
First look at the function declaration , This function is contained in time.h Header file , The return value type is time_t. Is from Epoch Seconds since . If the passed in parameter is not NULL, Then the seconds will be assigned to the passed in parameters .
#include <time.h>
time_t time(time_t *timep);
// returns number of seconds since the Epoch, or -1 on error
times() function
First of all times function , The statement is as follows . You can see times The return type of the function is clock_t, And its definition is contained in the header file sys/time.h in . You can call sysconf(_SC_CLK_TCK) To get real time . Pay attention here times Functions and the above mentioned time Functions have different functions ,times Function is a way to get the process time ,time Function is a way to get the clock time .
The prototype is as follows :
clock_t times(struct tms *buf);
tms The structure is as follows :
strace tms{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
notes :
tms_utime It records the time that the process executes user code .
tms_stime It records the time that the process executes the kernel code .
tms_cutime It records the time when the subprocess executes the user code .
tms_cstime It records when a child process executes kernel code
If we want to calculate Every time It needs to be used sysconf(_SC_CLK_TCK)) function , This sysconf Function can go to the information of the system . _SC_CLK_TCK Yes 1 second The number of clocks .
边栏推荐
- Cache avalanche and cache penetration solutions
- 安装onnx很慢,使用清华镜像
- “\“id\“ contains an invalid value“
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用scale_size函数配置数据点的大小的(size)度量调整的范围
- 治数如治水,数据治理和数据创新难在哪?
- He was the first hero of Shanghai's two major industries, but died silently in regret
- Parallel interface 8255A
- How can c write an SQL parser
- goto语句跳转未初始化变量:C2362
- Summer vacation study record
猜你喜欢
一个悄然崛起的国产软件,低调又强大!
wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记
MySQL 表的内连和外连
3D线光谱共焦传感器在半导体如何检测
NoSQL——Redis的配置与优化
服务器常用的一些硬件信息(不断更新)
AutoCAD - len command
网络营销之四大误解
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
随机推荐
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
The first batch in China! Alibaba cloud native data Lake products have passed the evaluation and certification of the ICT Institute
A high precision positioning approach for category support components with multiscale difference reading notes
1020. 飞地的数量
爱可可AI前沿推介(6.30)
Shutter start from zero 006 radio switches and checkboxes
YOLOv5导出onnx遇到的坑
Constructor, class member, destructor call order
Boost研究:Boost Log
Re understand oauth2.0 protocol for joint login
MATLAB中polarplot函数使用
NoSQL——Redis的配置与优化
再不上市,旷视科技就熬不住了
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
Multiparty cardinality testing for threshold private set-2021: Interpretation
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
服务器常用的一些硬件信息(不断更新)
Who still remembers "classmate Zhang"?
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
一瓶水引发的“战争”