如何在linux中使用c时间来打印函数运行时间?

时间:2011-11-23 09:38:51

标签: c linux time

当我在linux中运行c代码时,代码总是不会打印出已用时间,结果总是为0.代码如下:

#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);
}

5 个答案:

答案 0 :(得分:6)

不要忽视编译器警告;您尝试打印三个long变量(mtimesus),就像它们是double一样:

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’

sus更改为long,并将sus的格式更改为%ld,并将程序编译(并且无故障地运行。

答案 1 :(得分:6)

所有建议确实有效,但时间测量的粒度很大(通常为10到100毫秒)。所以它实际上测量了一些计算的东西,例如半秒钟。在当前的处理器上(运行速度为2到3Ghz,每个周期大约有3-5条指令),这意味着执行了十亿台机器指令(我们的C程序中的“基本步骤” - 通常有一个不明确的步骤概念)十几台机器说明)。所以你的测试太小了,你真的应该计算一百万次fibion​​acci(10)。

更具体地说,下面的程序(输出一些计算,以避免优化它们)在大约2秒内运行。 (关于少于16个的纤维蛋白计算的百万计算。)

#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)

答案 2 :(得分: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);

答案 3 :(得分:1)

实时时钟的分辨率可能不是很小(可能是10或25毫秒),而且你的计算时间太短而不显着。你可以把你的计算放在循环中(例如重复几千次)。

您还可以考虑使用clock功能测量CPU时间。

您还可以使用clock_gettime功能获得更好的结果。

正如其他人告诉您的那样,请gcc -Wall询问所有警告并将其考虑在内。如果您关注性能(但请记住过早优化是邪恶的,那么首先让您的程序正确!)考虑在编译期间启用优化(例如gcc -Wall -O2)。

答案 4 :(得分: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;
}