如何在linux中使用c time打印函数运行时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8239946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use c time in linux to print the function running time?
提问by Eric_Chen
when I run c code in linux,the code always doesnt print out the elapse time,and the result always is 0.The code is just as follow:
在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);
}
采纳答案by Basile Starynkevitch
All the suggestions do in fact work, but the granularity of the time measurement is big (typically 10 to 100 milliseconds). So it actually measure something for a computation which last e.g. half a second. On current processors (running at 2 to 3Ghz, with about 3-5 instructions per cycle), that means something like a billion machine instructions executed (an "elementary step" in our C program -with an ill-defined notion of step is usually a dozen machine instructions). So your test is too small, you really should compute a million times fibionacci (10).
所有的建议实际上都有效,但时间测量的粒度很大(通常为 10 到 100 毫秒)。所以它实际上测量了一些持续时间例如半秒的计算。在当前的处理器上(以 2 到 3Ghz 运行,每个周期大约有 3-5 条指令),这意味着执行了大约 10 亿条机器指令(我们 C 程序中的“基本步骤”——带有不明确定义的步骤概念通常是十几个机器指令)。所以你的测试太小了,你真的应该计算一百万次斐波那契 (10)。
To be more specific the program below (where some computations are output, to avoid optimizing them all) is running in about 2 seconds. (on million computations of fibionacci of something less than 16).
更具体地说,下面的程序(输出一些计算,以避免优化它们)在大约 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;
}
The last few lines output with time ./fib
(compiled with gcc -O2 -Wall fib.c -o fib
)
are
最后几行输出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
benchmarking a run smaller than about a second is not very meaningful
对小于约一秒的运行进行基准测试并不是很有意义
(and you can use the time
command to measure such a run)
(并且您可以使用该time
命令来测量这样的运行)
See also time(7)and clock_gettime(2).
另请参阅time(7)和clock_gettime(2)。
回答by Some programmer dude
回答by sarnold
Don't overlook your compiler warnings; you're trying to print three long
variables (mtime
, s
, and us
) as if they were double
s:
不要忽视你的编译器警告;您正在尝试打印三个long
变量 ( mtime
, s
, 和us
),就好像它们是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'
Change s
and us
to long
, and change the format for s
and us
to %ld
, and the program compiles (and runs) without fault.
变更s
和us
到long
,并更改格式s
,并us
以%ld
和程序编译(并运行)无故障。
回答by Basile Starynkevitch
The resolution of the real time clock is probably not very small (perhaps 10 or 25 milliseconds), and your computation is too short to be significant. You could put your computation inside a loop (e.g. repeating it several thousand times).
实时时钟的分辨率可能不是很小(可能是 10 或 25 毫秒),并且您的计算时间太短而无意义。你可以把你的计算放在一个循环中(例如重复几千次)。
You could also consider measuring the CPU time, using the clockfunction.
您还可以考虑使用时钟功能测量 CPU 时间。
You could also use the clock_gettimefunction to get perhaps better results.
您还可以使用clock_gettime函数来获得更好的结果。
And as other people told you, please ask for all warnings with gcc -Wall
and take them into account. If you care after performance (but remember that premature optimization is evil, so get your program right first!) consider enabling optimizations (e.g. gcc -Wall -O2
) during compilation.
正如其他人告诉你的那样,请询问所有警告gcc -Wall
并考虑它们。如果您关心性能(但请记住,过早优化是邪恶的,所以首先让您的程序正确!)考虑gcc -Wall -O2
在编译期间启用优化(例如)。
回答by Alessandro Pezzato
This should give you elapsed time:
这应该给你经过的时间:
#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(¤t, (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;
}