#ifndef _CLOCK_TIME_H_ #define _CLOCK_TIME_H_ #include <stdio.h> #include <time.h> #include <sys/time.h> unsigned long GetTickCount_ms(int clk_id); unsigned long GetTickCount_us(int clk_id); unsigned long GetTickCount_ns(int clk_id); #endif ================================================================ #include "clock_time.h" /* #include <time.h> 获取指定类型时钟的精确度 int clock_getres(clockid_t clk_id, struct timespec *res); 取得和设置clk_id指定的时间 int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_settime(clockid_t clk_id, const struct timespec *tp); Link with -lrt. CLOCK_BOOTTIME: 从系统启动这一刻开始计时,包括休眠时间,受到settimeofday的影响。 CLOCK_REALTIME System-wide real-time clock. Setting this clock requires appropriate privi‐ leges. 统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变 CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some unspecified starting point. 从系统启动这一刻起开始计时,不受系统时间被用户改变的影响 CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific) Similar to CLOCK_MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments. 与CLOCK_MONOTONIC一样,系统开启时计时,但不受NTP影响,受adjtime影响。 CLOCK_MONOTONIC_COARSE: 如同CLOCK_MONOTONIC,但有更快的获取速度和更低 一些的精确度。受NTP影响。 CLOCK_PROCESS_CPUTIME_ID High-resolution per-process timer from the CPU. 本进程到当前代码系统CPU花费的时间 CLOCK_THREAD_CPUTIME_ID Thread-specific CPU-time clock. 本线程到当前代码系统CPU花费的时间 return 0 for success, or -1 for failure 其他函数 date(1), adjtimex(2), gettimeofday(2), settimeofday(2), time(2), clock_getcpu‐ clockid(3), ctime(3), ftime(3), pthread_getcpuclockid(3), sysconf(3), time(7) */ /* struct timespec { time_t tv_sec; // 秒 long tv_nsec; // 纳秒 }; */ // 获得毫秒数 注:编译时需添加 -lrt unsigned long ClockGetTime_second(int clk_id) { struct timespec ts; clock_gettime(clk_id, &ts); return(ts.tv_sec+ts.tv_nsec/1000000000); } // 获得毫秒数 注:编译时需添加 -lrt unsigned long ClockGetTime_ms(int clk_id) { struct timespec ts; clock_gettime(clk_id, &ts); return(ts.tv_sec*1000+ts.tv_nsec/1000000); } // 获得微秒数 unsigned long ClockGetTime_us(int clk_id) { struct timespec ts; clock_gettime(clk_id, &ts); return(ts.tv_sec*1000000+ts.tv_nsec/1000); } // 获得纳秒数 unsigned long ClockGetTime_ns(int clk_id) { struct timespec ts; clock_gettime(clk_id, &ts); return(ts.tv_sec*1000*1000*1000+ts.tv_nsec); } #if 0 int main() { sleep(2); printf("TTT------------ms------------\n"); printf("TTT--CLOCK_REALTIME-----------[%lu]\n", GetTickCount_ms(CLOCK_REALTIME)); printf("TTT--CLOCK_MONOTONIC----------[%lu]\n", GetTickCount_ms(CLOCK_MONOTONIC)); printf("TTT--CLOCK_MONOTONIC_RAW------[%lu]\n", GetTickCount_ms(CLOCK_MONOTONIC_RAW)); printf("TTT--CLOCK_PROCESS_CPUTIME_ID-[%lu]\n", GetTickCount_ms(CLOCK_PROCESS_CPUTIME_ID)); printf("TTT--CLOCK_THREAD_CPUTIME_ID--[%lu]\n", GetTickCount_ms(CLOCK_THREAD_CPUTIME_ID)); printf("\nTTT------------us------------\n"); printf("TTT--CLOCK_REALTIME-----------[%lu]\n", GetTickCount_us(CLOCK_REALTIME)); printf("TTT--CLOCK_MONOTONIC----------[%lu]\n", GetTickCount_us(CLOCK_MONOTONIC)); printf("TTT--CLOCK_MONOTONIC_RAW------[%lu]\n", GetTickCount_us(CLOCK_MONOTONIC_RAW)); printf("TTT--CLOCK_PROCESS_CPUTIME_ID-[%lu]\n", GetTickCount_us(CLOCK_PROCESS_CPUTIME_ID)); printf("TTT--CLOCK_THREAD_CPUTIME_ID--[%lu]\n", GetTickCount_us(CLOCK_THREAD_CPUTIME_ID)); printf("\nTTT------------ns------------\n"); printf("TTT--CLOCK_REALTIME-----------[%lu]\n", GetTickCount_ns(CLOCK_REALTIME)); printf("TTT--CLOCK_MONOTONIC----------[%lu]\n", GetTickCount_ns(CLOCK_MONOTONIC)); printf("TTT--CLOCK_MONOTONIC_RAW------[%lu]\n", GetTickCount_ns(CLOCK_MONOTONIC_RAW)); printf("TTT--CLOCK_PROCESS_CPUTIME_ID-[%lu]\n", GetTickCount_ns(CLOCK_PROCESS_CPUTIME_ID)); printf("TTT--CLOCK_THREAD_CPUTIME_ID--[%lu]\n", GetTickCount_ns(CLOCK_THREAD_CPUTIME_ID)); return 0; } #endif |
|