2011-11-23 122 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中的方法,它真的起作用了,但是当我用它的时候就像我贴的代码一样。原来不行的 –

相关问题