温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux中怎么统计函数的执行时间

发布时间:2021-07-27 18:36:07 来源:亿速云 阅读:169 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关Linux中怎么统计函数的执行时间,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Linux下的函数执行时间的统计方法  

 

      如何测试某个函数的执行时间是做实验时经常用到的功能,在此比较Linux下的测试函数,主要是其精确度。我们采用统一的测试标准程序(standard.c): 

#include <stdio.h>

#define MAX 1000    /* the loop count */

/* function: do loop operation

 * input: NULL

 * output: counter->the counter result

 */

int do_work()

{      

    int counter = 0;    /* the counter */

    int i, j;                /* the loop variable */

    /* accumulate the counter */

    for(i = 0; i < MAX; i++)                           

        for(j = 0; j < MAX; j++)                                           

            counter++;             

    /* return the counter's value */

    return counter;

}

int main()

{

    printf("counter = %d\n", do_work());

}

通过命令gcc -o standard standard.c生成测试程序。

Linux下的方法:

(1)    使用命令time:

[root@localhost-120 xgf]# time ./standard

counter = 1000000

real    0m0.006s

user    0m0.000s

sys     0m0.000s

time命令对秒(s)级别的很精确,而对毫秒级的误差比价大。我们可以通过sleep/usleep函数来进行测试。sleep(0.1)或者usleep(100)都是表示休眠100ms,而测试结果都是:

real    0m0.002s

user    0m0.000s

sys     0m0.000s

(2)    通过difftime函数:

double difftime(time_t time1, time_t time0);计算time1和time0之间的秒数。测试程序如下:

#include <stdio.h>

#include <time.h>

#define MAX 1000

int do_work()

{

    int counter = 0;    /* the counter */

    int i, j;                    /* the loop variable */

    /* accumulate the counter */

    for(i = 0; i < MAX; i++)

        for(j = 0; j < MAX; j++)

            counter++;

    /* return the counter's value */

    return counter;

}

int main()

{

    time_t start, end;

    int val;

    start = time(NULL);

    do_work();

    end = time(NULL);

    printf("val = %f\n", difftime(end, start));

    return 0;

}

测试结果如下:

val = 0.000000

real    0m0.006s

user    0m0.000s

sys     0m0.000s

我们发现,difftime的精确度还没有time命令高。

(3)    通过gettimeofday函数:

int gettimeofday(struct timeval *tv, struct timezone *tz); 其中timeval结构定义如下:

struct timeval

{

             time_t      tv_sec;     /* seconds */

             suseconds_t tv_usec;    /* microseconds */

};

获取当前时刻,可以精确到微妙级别。

#include <stdio.h>

#include <sys/time.h>

#define MAX 1000    /* the loop count */

/* function: do loop operation

 * input: NULL

 * output: counter->the counter result

 */

int do_work()

{

    int counter = 0;    /* the counter */

    int i, j;           /* the loop variable */

    /* accumulate the counter */

    for(i = 0; i < MAX; i++)

        for(j = 0; j < MAX; j++)

            counter++;

    /* return the counter's value */

    return counter;

}

int main()

{

    struct timeval start, end;

    int interval;

    gettimeofday(&start, NULL);

    do_work();

    gettimeofday(&end, NULL);

    interval = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);

    printf("interval = %f\n", interval/1000.0);

}

输出结果如下:

interval = 3.527000

real    0m0.006s

user    0m0.000s

sys     0m0.000s

也就是3.527ms。

(4)    利用rdtsc汇编指令,这是硬件计数器提供的功能,可以精确到1/f(f为处理器频率)。

#include <stdio.h>

#include <time.h>

#define MAX 1000

#define FREQUENCE 1595984000.00 /* the frequence of CPU */

int do_work()

{

    int counter = 0;    /* the counter */

    int i, j;           /* the loop variable */

    /* accumulate the counter */

    for(i = 0; i < MAX; i++)

        for(j = 0; j < MAX; j++)

            counter++;

    /* return the counter's value */

    return counter;

}

int main()

{

    unsigned int start_high, start_low;

    unsigned int end_high, end_low;

    long long interval, start, end;

    /* get the start time */

    asm("rdtsc \n\t");

    asm("movl %%eax, %0\n\t":"=g"(start_low));

    asm("movl %%edx, %0\n\t":"=g"(start_high));

    printf("start_high:\t%08X start_low:\t%08X\n", start_high, start_low);

    start = start_high;

    start = (start << 32) | start_low;

    /* invoke the target function */

    do_work();

    /* get the end time */

    asm("rdtsc \n\t");

    asm("movl %%eax, %0\n\t":"=g"(end_low));

    asm("movl %%edx, %0\n\t":"=g"(end_high));

    printf("end_high:\t%08X end_low:\t%08X\n", end_high, end_low);

    end = end_high;

    end = (end << 32) | end_low;

    /* count the interval time */

    interval = end - start;

    printf("lost time is:\t%llX %f\n", interval, (interval * 1000)/FREQUENCE);

    return 0;

}

输出结果如下:

start_high:     00013272 start_low:     A1081568

end_high:       00013272 end_low:       A1600586

lost time is:   57F01E 3.611002

real    0m0.006s

user    0m0.000s

sys     0m0.000s

看完上述内容,你们对Linux中怎么统计函数的执行时间有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI