C语言时间函数(5)之clock_gettime()
程序员文章站
2024-01-21 21:22:46
...
1、clock_gettime(获取指定时钟的时间值)
#include <time.h>
int clock_gettime( clockid_t clock_id,struct timespec * tp );
说明:clock_id指定要获取时间的时钟,根据Posix的指定可以是以下值:
CLOCK_REALTIME 0
Systemwide realtime clock.
CLOCK_MONOTONIC 1
Represents monotonic time. Cannot be set.
CLOCK_PROCESS_CPUTIME_ID 2
High resolution per-process timer.
CLOCK_THREAD_CPUTIME_ID 3
Thread-specific timer.
CLOCK_REALTIME_HR 4
High resolution version of CLOCK_REALTIME.
CLOCK_MONOTONIC_HR 5
High resolution version of CLOCK_MONOTONIC.
获取数据的结构体定义如下:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds 纳秒*/
};
例子:
#include<stdio.h>
#include<time.h>
int main()
{
time_t timeval =0;
struct timespec st;
clock_gettime(CLOCK_REALTIME,&st);
time(&timeval);
printf("timeval = %ld\nclock_gettime val = %ld\n",timeval,st.tv_sec);
return 0;
}
结果:
当然还有其他很多函数,比如adjxtime(),比如
其他的实际用到的时候再学习。
推荐阅读
-
C语言时间函数(5)之clock_gettime()
-
pow函数(数学次方)在c语言的用法,两种编写方法实例( 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值)
-
socket C/C++编程(5)服务器端允许用户连接之listen()函数
-
C语言时间函数之strftime()详解
-
C语言时间函数之mktime和difftime详解
-
C语言时间函数的ctime()和gmtime()你了解吗
-
C语言程序设计100例之(5):分解质因数
-
C语言进阶学习之mian函数与命令行参数
-
C++学习(二十)(C语言部分)之 函数1
-
C语言时间函数的ctime()和gmtime()你了解吗