网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

成都创新互联是一家集网站建设,下冶企业网站建设,下冶品牌网站建设,网站定制,下冶网站建设报价,网络营销,网络优化,下冶网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

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

 

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

#include

#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

#include

#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

#include

#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

#include

#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中怎么统计函数的执行时间有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


名称栏目:Linux中怎么统计函数的执行时间
标题路径:http://bjjierui.cn/article/ieposh.html

其他资讯