您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Linux中怎么統(tǒng)計(jì)函數(shù)的執(zhí)行時(shí)間,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
如何測試某個(gè)函數(shù)的執(zhí)行時(shí)間是做實(shí)驗(yàn)時(shí)經(jīng)常用到的功能,在此比較Linux下的測試函數(shù),主要是其精確度。我們采用統(tǒng)一的測試標(biāo)準(zhǔn)程序(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命令對(duì)秒(s)級(jí)別的很精確,而對(duì)毫秒級(jí)的誤差比價(jià)大。我們可以通過sleep/usleep函數(shù)來進(jìn)行測試。sleep(0.1)或者usleep(100)都是表示休眠100ms,而測試結(jié)果都是:
real 0m0.002s
user 0m0.000s
sys 0m0.000s
(2) 通過difftime函數(shù):
double difftime(time_t time1, time_t time0);計(jì)算time1和time0之間的秒數(shù)。測試程序如下:
#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;
}
測試結(jié)果如下:
val = 0.000000
real 0m0.006s
user 0m0.000s
sys 0m0.000s
我們發(fā)現(xiàn),difftime的精確度還沒有time命令高。
(3) 通過gettimeofday函數(shù):
int gettimeofday(struct timeval *tv, struct timezone *tz); 其中timeval結(jié)構(gòu)定義如下:
struct timeval
{
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
獲取當(dāng)前時(shí)刻,可以精確到微妙級(jí)別。
#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);
}
輸出結(jié)果如下:
interval = 3.527000
real 0m0.006s
user 0m0.000s
sys 0m0.000s
也就是3.527ms。
(4) 利用rdtsc匯編指令,這是硬件計(jì)數(shù)器提供的功能,可以精確到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;
}
輸出結(jié)果如下:
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
看完上述內(nèi)容,你們對(duì)Linux中怎么統(tǒng)計(jì)函數(shù)的執(zhí)行時(shí)間有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。