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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3946842/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:42:52  来源:igfitidea点击:

Measuring time taken by a function: clock_gettime

ctimingclock

提问by Jary

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:

我有一个小问题:尽管我试图精确并使用浮点数,但每次我使用 %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 );

Most of this code has not been made by me. This example page had code illustrating the use of clock_gettime: http://www.users.pjwstk.edu.pl/~jms/qnx/help/watcom/clibref/qnx/clock_gettime.html

大部分代码不是我制作的。此示例页面包含说明 clock_gettime 使用的代码:http://www.users.pjwstk.edu.pl/~jms/qnx/help/watcom/clibref/qnx/clock_gettime.html

Could anyone please let me know what is incorrect, or why I am only getting integer values please?

任何人都可以让我知道什么是不正确的,或者为什么我只得到整数值?

Thank you very much,

非常感谢,

Jary

贾里

采纳答案by Marcelo Cantos

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. #defineis 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.

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

回答by Oliver Charlesworth

( 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. doublebefore doing the divide.

( requestEnd.tv_nsec - requestStart.tv_nsec )是整数类型,并且总是小于BILLION,所以在整数算术中将一个除以另一个的结果将始终是0double在进行除法之前,您需要将减法的结果转换为 eg 。

回答by AntonK

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 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...

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

回答by Wayfarer

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.

请注意, (requestEnd.tv_nsec - requestStart.tv_nsec) 可以为负数,在这种情况下,您需要从 tv_sec 差异中减去 1 秒,并将 tv_nsec 差异增加 10 亿。