clock_gettime( ) 提供了納秒的精確度,給程序計時可是不錯哦;
函數的原型如下:
int clock_gettime(clockid_t clk_id, struct timespect *tp);
clockid_t
clk_id用於指定計時時鐘的類型,對於我們Programmr以下三種比較常用:
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_MONOTONIC
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
struct timespect
*tp用來存儲當前的時間,其結構如下:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
原文http://igaozh.iteye.com/blog/1675767