2011-11-23 125 views
2

當我運行在Linux下C代碼,這個代碼總是犯規打印出經過時間,而結果總是0.The代碼只是如下:如何在linux中使用c時間來打印函數運行時間?

#include <sys/time.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
void main(int argc,char* argv[]){ 
    int n; 
    if(argc == 2){ 
    n = atoi(argv[1]); 
    } 
    struct timeval start, end; 
    gettimeofday(&start, 0); 
    int r = fib(n); 
    gettimeofday(&end, 0); 
    long mtime, s,us; 
    s = end.tv_sec - start.tv_sec; 
    us = end.tv_usec - start.tv_usec; 
    printf("s=%f,us=%f \n", s, us); 
    mtime = (s*1000 + us/1000.0)+0.5; 
    printf("Fib result for %d is: %d;elapsing %f \n", n, r, mtime); 

} 

int fib(int n){ 
    if(n == 0) return 0; 
    if(n == 1) return 1; 
    return fib(n-1)+fib(n-2); 
} 

回答

6

所有的建議做事實上的工作,但時間測量的粒度大(通常爲10至100毫秒)。所以它實際上是爲了計算最後的半秒鐘。在當前的處理器上(運行在2到3Ghz之間,每個週期約有3-5條指令),這意味着有10億條機器指令被執行(我們C程序中的「基本步驟」) - 通常沒有明確的步驟概念一打機器說明書)。所以你的測試太小了,你真的應該計算一百萬次fibion​​acci(10)。

更具體地說,下面的程序(其中輸出一些計算,以避免優化它們)在大約2秒內運行。 (對小於16的fibion​​acci進行百萬次計算)。

#include <stdio.h> 
#include <unistd.h> 
#include <time.h> 
long fib(int n){ 
    if(n == 0) return 0; 
    if(n == 1) return 1; 
    return fib(n-1)+fib(n-2); 
} 

int main() 
{ 
    int i=0; 
    int p = (int) getpid(); 
    clock_t cstart = clock(); 
    clock_t cend = 0; 
    for (i=0; i<1000000; i++) { 
    long f = fib(i%16); 
    if (i % p == 0) printf("i=%d, f=%ld\n", i, f); 
    } 
    cend = clock(); 
    printf ("%.3f cpu sec\n", ((double)cend - (double)cstart)* 1.0e-6); 
    return 0; 
} 

最後輸出幾行time ./fib(編譯gcc -O2 -Wall fib.c -o fib) 是

i=936079, f=610 
i=948902, f=8 
i=961725, f=233 
i=974548, f=3 
i=987371, f=89 
2.140 cpu sec 
./fib 2.15s user 0.00s system 99% cpu 2.152 total 

基準測試運行超過一秒鐘更小的意義不大

(你可以使用time命令來測量這樣的運行)

另請參閱time(7)clock_gettime(2)

+0

Excellect的工作,thx很多Starynkevitch –

2

這可能是更容易使用的clock功能:

clock_t start = clock(); 
int r = fib(n); 
clock_t end = clock(); 
printf("Elapsed time: %.2f seconds\n", (double)(end - start)/CLOCKS_PER_SEC); 
+0

'clock()'給你處理器使用的時間或經過的時間? –

+0

@AlessandroPezzato根據手冊頁(鏈接到答案中):clock()函數返回程序使用的處理器時間的近似值。 –

+2

'clock'測量CPU時間,而不是掛鐘時間。這可能不是問題所在。 – ibid

6

不要忽視你的編譯器警告;你想打印方式中三個long變量(mtimesus),如果他們double S:

fib.c: In function ‘main’: 
fib.c:17:3: warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘long int’ 
fib.c:17:3: warning: format ‘%f’ expects type ‘double’, but argument 3 has type ‘long int’ 
fib.c:19:3: warning: format ‘%f’ expects type ‘double’, but argument 4 has type ‘long int’ 

變化suslong,並更改格式sus%ld,和該程序編譯(並運行)沒有錯誤。

1

實時時鐘的分辨率可能不是非常小(可能爲10或25毫秒),而且您的計算太短而不重要。你可以把你的計算放在一個循環中(例如重複它幾千次)。

您還可以考慮使用clock函數來測量CPU時間。

您也可以使用clock_gettime函數來獲得更好的結果。

正如其他人告訴你的,請要求所有警告與gcc -Wall並考慮到他們。如果你關心性能(但請記住,過早優化是邪惡的,所以先讓你的程序正確!)考慮在編譯期間啓用優化(例如gcc -Wall -O2)。

+0

說實話,我可以給斐波那契函數一個很好的數字,但即使我給它一個數字,如50,結果時間仍將是0.0000 –

+0

您是否重複了一次對斐波那契的調用? –

0

這應該給你經過時間:

#include <iostream> 
#include <sys/time.h> /* gettimeofday */ 

int main() { 
    /* get begin time */ 
    timeval begin; 
    ::gettimeofday(&begin, 0); 
    /* do something... */ 
    ::usleep(153); 
    /* get end time */ 
    ::timeval current; 
    ::gettimeofday(&current, (struct timezone*) 0); 
    /* calculate difference */ 
    double elapsed = (current.tv_sec - begin.tv_sec) + ((current.tv_usec 
      - begin.tv_usec)/1000000.0F); 
    /* print it */ 
    std::cout << elapsed << std::endl; 
    return 0; 
} 
+0

我試過c中的方法,它真的起作用了,但是當我用它的時候就像我貼的代碼一樣。原來不行的 –

相關問題