Linux下时间度量的深入分析

47 篇文章 0 订阅

 


一)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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值