测量函数所花费的时间:clock_gettime
我正在尝试测量一个函数需要多长时间。
我有一个小问题:虽然我试图精确并使用浮点,但每次我使用 %lf
打印代码时,我都会得到两个答案之一:1.000
... 或 0.000
... 这让我想知道我的代码是否正确:
#define BILLION 1000000000L;
// Calculate time taken by a request
struct timespec requestStart, requestEnd;
clock_gettime(CLOCK_REALTIME, &requestStart);
function_call();
clock_gettime(CLOCK_REALTIME, &requestEnd);
// Calculate time it took
double accum = ( requestEnd.tv_sec - requestStart.tv_sec )
+ ( requestEnd.tv_nsec - requestStart.tv_nsec )
/ BILLION;
printf( "%lf\n", accum );
大部分代码都不是我编写的。此示例页面的代码说明了 clock_gettime
:
任何人都可以告诉我什么是不正确的,或者为什么我只获取 int
值吗?
I am trying to measure how long a function takes.
I have a little issue: although I am trying to be precise, and use floating points, every time I print my code using %lf
I get one of two answers: 1.000
... or 0.000
... This leads me to wonder if my code is correct:
#define BILLION 1000000000L;
// Calculate time taken by a request
struct timespec requestStart, requestEnd;
clock_gettime(CLOCK_REALTIME, &requestStart);
function_call();
clock_gettime(CLOCK_REALTIME, &requestEnd);
// Calculate time it took
double accum = ( requestEnd.tv_sec - requestStart.tv_sec )
+ ( requestEnd.tv_nsec - requestStart.tv_nsec )
/ BILLION;
printf( "%lf\n", accum );
Most of this code has not been made by me. This example page had code illustrating the use of clock_gettime
:
Could anyone please let me know what is incorrect, or why I am only getting int
values please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将一个整数除以一个整数得到一个整数。试试这个:
不要在行尾使用分号。
#define
是一个预处理器指令,而不是语句,并且包含分号会导致BILLION
被定义为1000000000L;
,如果您尝试在大多数情况下使用它。你很幸运,因为你在表达式的最后和任何括号之外使用了它。Dividing an integer by an integer yields an integer. Try this:
And don't use a semicolon at the end of the line.
#define
is a preprocessor directive, not a statement, and including the semicolon resulted inBILLION
being defined as1000000000L;
, which would break if you tried to use it in most contexts. You got lucky because you used it at the very end of an expression and outside any parentheses.( requestEnd.tv_nsec - requestStart.tv_nsec )
是整数类型,并且始终小于BILLION
,因此整数运算中一个除以另一个的结果始终为<代码>0。在进行除法之前,您需要将减法的结果转换为例如double
。( requestEnd.tv_nsec - requestStart.tv_nsec )
is of integer type, and is always less thanBILLION
, so the result of dividing one by the other in integer arithmetic will always be0
. You need to cast the result of the subtraction to e.g.double
before doing the divide.请注意,(requestEnd.tv_nsec - requestStart.tv_nsec) 可以为负数,在这种情况下,您需要从 tv_sec 差值中减去 1 秒,并在 tv_nsec 差值上添加 10 亿。
Note that (requestEnd.tv_nsec - requestStart.tv_nsec) can be negative, in which case you need to subtract 1 second from the tv_sec difference and add one BILLION to the tv_nsec difference.
我知道这个问题很久以前就发布了,但我仍然没有看到建议您将经过的时间“转换”为纳秒(或毫秒)而不是像代码示例中那样转换为秒的答案。
示例代码片段说明了这个想法:
这样您就可以避免浮点运算,这对于某些平台来说可能很重......
I know the question was posted long ago, but I still don't see the answer which would suggest you to "convert" elapsed time into nanoseconds (or milliseconds) and not into seconds as in your code sample.
The sample code fragment to illustrate the idea:
This way you can avoid floating point arithmetic, which may be heavy for some platforms...