Linux中怎么统计函数的执行时间
创始人
2024-12-26 00:42:37
0次

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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...