C语言时间函数(5)之clock_gettime()
2018-01-11 22:28
567 查看
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 纳秒*/
};
例子:
结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/05/a8b7ee496321ae6fb4f3e366f3b8fb41)
当然还有其他很多函数,比如adjxtime(),比如
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/05/e36af6aca0bcbee9d898c390a4327465)
其他的实际用到的时候再学习。
#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(),比如
其他的实际用到的时候再学习。
相关文章推荐
- linux常用的时间获取函数(time,gettimeofday,clock_gettime,_ftime,localtime,strftime )
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- C语言中的一些时间函数(time/sleep/clock)
- 计算程序运行时间 C语言 clock()函数
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- 利用clock()函数计算一段代码运行消耗的时间(C语言)
- linux 上时间相关的函数 time ,_ftime , gettimeofday, clock_gettime
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- linux常用时间函数:time,gettimeofday,clock_gettime,ftime
- C语言之时间函数 分类: C语言 2014-12-03 12:57 147人阅读 评论(0) 收藏
- C语言的时间函数(1)gettimeofday,timeval,timezone
- C语言gettimeofday()函数:获取当前时间
- C语言的时间函数
- c语言中时间函数精度
- c语言中如何添加计时函数,统计程序执行的时间
- c语言获取时间函数整理
- C语言格式化输出时间函数strftime()
- C语言 时间函数
- C语言中格式化日期时间ctime()函数详细讲解
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别