Linux下時間度量的深入分析

來源:互聯網
上載者:User

 


一)ANSI clock函數

1)概述:
clock 函數的傳回值類型是clock_t,它除以CLOCKS_PER_SEC來得出時間,一般用兩次clock函數來計算進程自身啟動並執行時間.

ANSI clock有三個問題:
1)如果超過一個小時,將要導致溢出.
2)函數clock沒有考慮CPU被子進程使用的情況.
3)也不能區分使用者空間和核心空間.

所以clock函數在linux系統上變得沒有意義.

2)測試
編寫test1.c程式,測試採用clock函數的輸出與time程式的區別.

vi test1.c 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>

int main( void ) 

   long i=1000L; 
   clock_t start, finish; 
   double  duration; 
   printf( "Time to do %ld empty loops is ", i ); 
   start = clock(); 
   while (--i){
    system("cd");
   }
   finish = clock(); 
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf( "%f seconds ", duration ); 
   return 0;
}

gcc test1.c -o test1

time ./test1
Time to do 1000 empty loops is 0.180000 seconds

real    0m3.492s
user    0m0.512s
sys     0m2.972s

3)總結:
(1)程式調用 system("cd");,這裡主要是系統模式子進程的消耗,test1程式不能體現這一點.
(2)0.180000 seconds秒的消耗是兩次clock()函數調用除以CLOCKS_PER_SEC.
(3)clock()函數傳回值是一個相對時間,而不是絕對時間.
(4)CLOCKS_PER_SEC是系統定義的宏,由GNU標準庫定義為1000000.

 

二)times()時間函數

1)概述:

原型如下:
clock_t times(struct tms *buf);

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 ", real/(double)clktck);
        printf("user-cpu:%7.2f ", (tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);
        printf("system-cpu:%7.2f ", (tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);
        printf("child-user-cpu:%7.2f ", (tmsend->tms_cutime - tmsstart->tms_cutime)/(double)clktck);
        printf("child-system-cpu:%7.2f ", (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

real    0m4.943s
user    0m0.016s
sys     0m4.828s

3)總結:
(1)通過這個測試,系統的time程式與test2程式輸出基本一致了.
(2)(double)clktck是通過clktck=sysconf(_SC_CLK_TCK)來取的,也就是要得到user-cpu所佔用的時間,就要用
(tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);
(3)clock_t times(struct tms *buf);傳回值是過去一段時間內時鐘嘀嗒的次數.
(4)times()函數傳回值也是一個相對時間.

 

三)即時函數clock_gettiem

在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 ", timedif);

        return 0;
}

編譯:
gcc test3.c -lrt -o test3

計算時間:
time ./test3 
it took 3463843 microseconds

real    0m3.467s
user    0m0.512s
sys     0m2.936s

 

四)時間函數gettimeofday()

1)概述:
gettimeofday()可以獲得當前系統的時間,是一個絕對值

原型如下:
int gettimeofday ( struct timeval * tv , struct timezone * tz )

timeval結型體的原型如下:
struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };

所以它可以精確到微秒

測試:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
int
main(){
        int i=10000000;
        struct timeval tvs,tve;
        gettimeofday(&tvs,NULL);
        while (--i);
        gettimeofday(&tve,NULL);
        double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
        printf("time: %.12f ",span);
        return 0;
}

gcc test5.c
./a.out 
time: 0.041239000000

 

五)四種時間函數的比較

1)精確度比較:

以下是各種精確度的類型轉換:
1秒=1000毫秒(ms), 1毫秒=1/1000秒(s); 
1秒=1000000 微秒(μs), 1微秒=1/1000000秒(s); 
1秒=1000000000 納秒(ns),1納秒=1/1000000000秒(s);

2)
clock()函數的傳回值為毫秒(ms)
times()函數的傳回值也是毫秒(ms)
gettimofday()函數的傳回值是微秒(μs)
clock_gettime()函數的計量單位為十億分之一,也就是納秒(ns)

3)測試4種函數的精確度:

vi test4.c

#include    <stdio.h>
#include    <stdlib.h>
#include    <unistd.h>
#include    <time.h>
#include    <sys/times.h>
#include    <sys/time.h>
#define WAIT for(i=0;i<298765432;i++);
#define MILLION    1000000
    int
main ( int argc, char *argv[] )
{
    int i;
    long ttt;
    clock_t s,e;
    struct tms aaa;

/*  use clock() function */
    s=clock();
    WAIT;
    e=clock();
    printf("clock time : %.12f ",(e-s)/(double)CLOCKS_PER_SEC);

/*  use times() function */
    long tps = sysconf(_SC_CLK_TCK);
    s=times(&aaa);
    WAIT;
    e=times(&aaa);
    printf("times time : %.12f ",(e-s)/(double)tps);

/*  use gettimeofday() function */
    struct timeval tvs,tve;
    gettimeofday(&tvs,NULL);
    WAIT;
    gettimeofday(&tve,NULL);
    double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
    printf("gettimeofday time: %.12f ",span);

/*  use clock_gettime() function */
    struct timespec tpstart;
    struct timespec tpend;

    clock_gettime(CLOCK_REALTIME, &tpstart);
    WAIT;
    clock_gettime(CLOCK_REALTIME, &tpend);
    double timedif = (tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000000000.0;
    printf("clock_gettime time: %.12f ", timedif);

    return EXIT_SUCCESS;
}

gcc -lrt test4.c -o test4
debian:/tmp# ./test4
clock time : 1.190000000000
times time : 1.180000000000
gettimeofday time: 1.186477000000
clock_gettime time: 1.179271718000

 

 

轉自:http://home.lupaworld.com/home.php?mod=space&uid=56821&do=blog&id=138299

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.