测量函数所花费的时间:clock_gettime

发布于 2024-09-28 09:01:48 字数 827 浏览 2 评论 0原文

我正在尝试测量一个函数需要多长时间。

我有一个小问题:虽然我试图精确并使用浮点,但每次我使用 %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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离笑几人歌 2024-10-05 09:01:48

将一个整数除以一个整数得到一个整数。试试这个:

#define BILLION 1E9

不要在行尾使用分号。 #define 是一个预处理器指令,而不是语句,并且包含分号会导致 BILLION 被定义为 1000000000L;,如果您尝试在大多数情况下使用它。你很幸运,因为你在表达式的最后和任何括号之外使用了它。

Dividing an integer by an integer yields an integer. Try this:

#define BILLION 1E9

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 in BILLION being defined as 1000000000L;, 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.

柠檬 2024-10-05 09:01:48

( requestEnd.tv_nsec - requestStart.tv_nsec ) 是整数类型,并且始终小于 BILLION,因此整数运算中一个除以另一个的结果始终为<代码>0。在进行除法之前,您需要将减法的结果转换为例如double

( requestEnd.tv_nsec - requestStart.tv_nsec ) is of integer type, and is always less than BILLION, so the result of dividing one by the other in integer arithmetic will always be 0. You need to cast the result of the subtraction to e.g. double before doing the divide.

羁客 2024-10-05 09:01:48

请注意,(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.

聚集的泪 2024-10-05 09:01:48

我知道这个问题很久以前就发布了,但我仍然没有看到建议您将经过的时间“转换”为纳秒(或毫秒)而不是像代码示例中那样转换为秒的答案。

示例代码片段说明了这个想法:

long long accum = ( requestEnd.tv_nsec - requestStart.tv_nsec )
 + ( requestEnd.tv_sec - requestStart.tv_sec ) * BILLION;

这样您就可以避免浮点运算,这对于某些平台来说可能很重......

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:

long long accum = ( requestEnd.tv_nsec - requestStart.tv_nsec )
 + ( requestEnd.tv_sec - requestStart.tv_sec ) * BILLION;

This way you can avoid floating point arithmetic, which may be heavy for some platforms...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文