您的位置:首页 > 运维架构 > Linux

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

2014-08-22 14:45 483 查看
如何测试某个函数的执行时间是做实验时经常用到的功能,在此比较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
综上所述,time命令和difftime函数基本都是秒级别的,根本达不到毫秒级别的计数。而gettimeofday和rdtsc是很精确的方式,建议如果大家以后需要毫秒级别的计数采用gettimeofday或者rdtsc。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: