linux獲取時間函式及計算時間差
參考: http://www.cnblogs.com/krythur/archive/2013/02/25/2932647.html
第一章 獲取時間函式
1. char * asctime(const struct tm * timeptr);
函式說明
asctime()將引數timeptr所指的tm結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果以字串形態返回。此函式已經由時區轉換成當地時間,字串格式為:“Wed Jun 30 21:49:08 1993\n”
返回值
若再呼叫相關的時間日期函式,此字串可能會被破壞。此函式與ctime不同處在於傳入的引數是不同的結構。
附加說明
返回一字串表示目前當地的時間日期。
範例
#include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
執行
Sat Oct 28 02:10:06 2000
2. 定義函式
char *ctime(const time_t *timep);
函式說明
ctime()將引數timep所指的time_t結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果以字串形態返回。此函式已經由時區轉換成當地時間,字串格式為“Wed Jun 30 21 :49 :08 1993\n”。若再呼叫相關的時間日期函式,此字串可能會被破壞。
返回值
返回一字串表示目前當地的時間日期。
範例
#include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}
執行
Sat Oct 28 10 : 12 : 05 2000
3. struct tm*gmtime(const time_t*timep);
函式說明
gmtime()將引數timep 所指的time_t 結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
結構tm的定義為
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒數,正常範圍為0-59,但允許至61秒
int tm_min 代表目前分數,範圍0-59
int tm_hour 從午夜算起的時數,範圍為0-23
int tm_mday 目前月份的日數,範圍01-31
int tm_mon 代表目前月份,從一月算起,範圍從0-11
int tm_year 從1900 年算起至今的年數
int tm_wday 一星期的日數,從星期一算起,範圍為0-6
int tm_yday 從今年1月1日算起至今的天數,範圍為0-365
int tm_isdst 日光節約時間的旗標
此函式返回的時間日期未經時區轉換,而是UTC時間。
返回值
返回結構tm代表目前UTC 時間
範例
#include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
執行
2000/10/28 Sat 8:15:38
4. struct tm *localtime(const time_t * timep);
函式說明
localtime()將引數timep所指的time_t結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。結構tm的定義請參考gmtime()。此函式返回的時間日期已經轉換成當地時區。
返回值
返回結構tm代表目前的當地時間。
範例
#include<time.h>
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得當地時間*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
執行
2000/10/28 Sat 11:12:22
5. time_t mktime(strcut tm * timeptr);
函式說明
mktime()用來將引數timeptr所指的tm結構資料轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
範例
/* 用time()取得時間(秒數),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d \n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
執行
time():974943297
time()->localtime()->mktime():974943297
6. time_t time(time_t *t);
函式說明
此函式會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指標的話,此函式也會將返回值存到t指標所指的記憶體。
返回值
成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存於errno中。
範例
#include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
7. gettimeofday() :可獲得微妙級(0.000001秒)的系統時間,呼叫兩次gettimeofday(),前後做減法,從而達到定時或者計算時間的目的。
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep); //獲取的為英國時間
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep); //獲取的為本地時間,注意與英國時間的區別。
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
double difftime(time_t time1, time_t time0);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
第二章 計算時間差
1. 實時函式clock_gettime 單位是十億分之一秒,也就是納秒(ns),使用的是標準POSIX實時時鐘, 計算出來的結果可能有誤差。
在POSIX1003.1中增添了這個函式,它的原型如下:
int clock_gettime(clockid_t clk_id, struct timespec *tp);
它有以下的特點:
1)它也有一個時間結構體:timespec ,timespec計算時間次數的單位是十億分之一秒.
strace timespec{
time_t tv_sec;
long tv_nsec;
}
2)clockid_t是確定哪個時鐘型別.
CLOCK_REALTIME: 標準POSIX實時時鐘
CLOCK_MONOTONIC: POSIX時鐘,以恆定速率執行;不會復位和調整,它的取值和CLOCK_REALTIME是一樣的.
CLOCK_PROCESS_CPUTIME_ID和CLOCK_THREAD_CPUTIME_ID是CPU中的硬體計時器中實現的.
3)測試:
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#define MILLION 1000000
int main(void)
{
long int loop = 1000;
struct timespec tpstart;
struct timespec tpend;
long timedif;
clock_gettime(CLOCK_MONOTONIC, &tpstart);
while (--loop){
system("cd");
}
clock_gettime(CLOCK_MONOTONIC, &tpend);
timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
fprintf(stdout, "it took %ld microseconds\n", timedif);
return 0;
}
編譯:
gcc test3.c -lrt -o test3
計算時間:
time ./test3
it took 3463843 microseconds
2. int gettimeofday(struct timeval *tv,struct timezone*tz),會把目前的時間tv所指的結構返回,當地時區的資訊則放到tz所指的結構中。這兩個結構都放在/usr/include/sys/time.h中。
單位 微秒(μs)
#include <stdio.h>
#include <stdlib.h> //malloc要用,沒有的話,會有警告資訊:隱式宣告與內建函式'malloc'不相容。不過警告資訊不用管也沒事
#include <assert.h>
#include <sys/time.h>
int main()
{
float time_use=0;
struct timeval start;
struct timeval end;
//struct timezone tz; //後面有說明
gettimeofday(&start,NULL);//gettimeofday(&start,&tz);結果一樣
printf("start.tv_sec:%d\n",start.tv_sec);
printf("start.tv_usec:%d\n",start.tv_usec);
sleep(3);
gettimeofday(&end,NULL);
printf("end.tv_sec:%d\n",end.tv_sec);
printf("end.tv_usec:%d\n",end.tv_usec);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %f\n",time_use);
//輸出:time_use is 3001410.000000
//下面的採用指標的方式也可以,但是要注意指標型別若不分配記憶體的話,編譯正確,但是執行結果會不對
}
3. #include <sys/times.h>
clock_t
times(struct
tms *buf);
單位10毫秒(ms) times實際上面就是呼叫clock() 實現的。
times() 函式返回從過去一個任意的時間點所經過的時鐘數。返回值可能會超出 clock_t (一般為 long 型) 的範圍(溢位)。如果發生錯誤,則返回 (clock_t ) -1 型別,然後設定相應的 errno 值。
系統每秒的時鐘可以通過 sysconf(_SC_CLK_TCK); 函式獲得。
tms結構體如下:
strace tms{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
註釋:
tms_utime記錄的是程序執行使用者程式碼的時間.
tms_stime記錄的是程序執行核心程式碼的時間.
tms_cutime記錄的是子程序執行使用者程式碼的時間.
tms_cstime記錄的是子程序執行核心程式碼的時間.
2)測試:
vi test2.c
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void do_cmd(char *);
static void pr_times(clock_t, struct tms *, struct tms *);
int main(int argc, char *argv[]){
int i;
for(i=1; argv[i]!=NULL; i++){
do_cmd(argv[i]);
}
exit(1);
}
static void do_cmd(char *cmd){
struct tms tmsstart, tmsend;
clock_t start, end;
int status;
if((start=times(&tmsstart))== -1)
puts("times error");
if((status=system(cmd))<0)
puts("system error");
if((end=times(&tmsend))== -1)
puts("times error");
pr_times(end-start, &tmsstart, &tmsend);
exit(0);
}
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend){
static long clktck=0;
if(0 == clktck)
if((clktck=sysconf(_SC_CLK_TCK))<0)
puts("sysconf err");
printf("real:%7.2f\n", real/(double)clktck);
printf("user-cpu:%7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);
printf("system-cpu:%7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);
printf("child-user-cpu:%7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime)/(double)clktck);
printf("child-system-cpu:%7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime)/(double)clktck);
}
編譯:
gcc test2.c -o test2
測試這個程式:
time ./test2 "dd if=/dev/zero f=/dev/null bs=1M count=10000"
10000+0 records in
10000+0 records out
10485760000 bytes (10 GB) copied, 4.93028 s, 2.1 GB/s
real: 4.94
user-cpu: 0.00
system-cpu: 0.00
child-user-cpu: 0.01
child-system-cpu: 4.82
4. #include <time.h>
clock_t clock(void); 單位10毫秒(ms)
int clock_gettime(clockid_t clk_id, struct timespec *tp);
系統每秒的時鐘可以通過 sysconf(_SC_CLK_TCK); 函式獲得。
第三章 總結
1)精確度比較:
以下是各種精確度的型別轉換:
1秒=1000毫秒(ms), 1毫秒=1/1000秒(s);
1秒=1000000 微秒(μs), 1微秒=1/1000000秒(s);
1秒=1000000000 納秒(ns),1納秒=1/1000000000秒(s);
2)
clock()函式的精確度是10毫秒(ms)
times()函式的精確度是10毫秒(ms)
gettimofday()函式的精確度是微秒(μs)
clock_gettime()函式的計量單位為十億分之一,也就是納秒(ns)
3) times() 和 clock()
預設的Linux時鐘週期是100HZ,而現在最新的核心時鐘週期預設為250HZ.
如何得到核心的時鐘週期呢?
grep ^CONFIG_HZ /boot/config-2.6.26-1-xen-amd64
CONFIG_HZ_250=y
CONFIG_HZ=250
結果就是250HZ.
而用sysconf(_SC_CLK_TCK);得到的卻是100HZ
例如:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>
#include <sys/time.h>
int
main ( int argc, char *argv[] )
{
long tps = sysconf(_SC_CLK_TCK);
printf("%ld\n", tps);
return EXIT_SUCCESS;
}
為什麼得到的是不同的值呢?
因為sysconf(_SC_CLK_TCK)和CONFIG_HZ所代表的意義是不同的.
sysconf(_SC_CLK_TCK)是GNU標準庫的clock_t頻率.
它的定義位置在:/usr/include/asm/param.h
例如:
#ifndef HZ
#define HZ 100
#endif
最後總結一下核心時間:
核心的標準時間是jiffy,一個jiffy就是一個內部時鐘週期,而內部時鐘週期是由250HZ的頻率所產生中的,也就是一個時鐘滴答,間隔時間是4毫秒(ms).
也就是說:
1個jiffy=1個內部時鐘週期=250HZ=1個時鐘滴答=4毫秒
sysconf(_SC_CLK_TCK)使用預設的Linux時鐘週期是100HZ, 1個jiffy=1個內部時鐘週期=100HZ=1個時鐘滴答=10毫秒,所以clock() 和 times()的最新單位是10ms
每經過一個時鐘滴答就會呼叫一次時鐘中斷處理程式,處理程式用jiffy來累計時鐘滴答數,每發生一次時鐘中斷就增1.
而每個中斷之後,系統通過排程程式跟據時間片選擇是否要程序繼續執行,或讓程序進入就緒狀態.