简体   繁体   English

在 C 中测量经过的时间?

[英]Measure elapsed time in C?

So, I'd like to see how long a function of in my code takes to run.所以,我想看看我的代码中的 function 需要多长时间才能运行。 (in realtime). (实时)。 Originally, I had this:最初,我有这个:

clock_t begin = clock();
my_function();
clock_t end = clock();
double time_spent = (double)(end - begin);

But apparently, there are some problems with this approach.但显然,这种方法存在一些问题。

  1. It only measures the time that the cpu took to process my application.它只测量 cpu 处理我的应用程序所花费的时间。
  2. It only measures on microsecond level, from what I can see - which isn't precise enough for my case.据我所见,它仅在微秒级别进行测量 - 这对我的情况来说不够精确。

So, what is the proper way to get the time a function took to run?那么,获得 function 运行时间的正确方法是什么? Is CPU time really the right approach? CPU时间真的是正确的方法吗? How precise can I measure?我可以测量多精确? I was thinking nanosecond level?我在想纳秒级?

The C Standard does not define a portable way to do this. C 标准没有定义可移植的方式来执行此操作。 The time() library function has a definition of 1 second, which is inappropriate for your purpose. time()库 function 的定义为 1 秒,这不适合您的目的。 As mentioned by @Puck, C11 did introduce timespec_get() to retrieve a more precise time value, but this function is not widely supported and may not provide the expected accuracy.正如@Puck 所提到的,C11 确实引入了timespec_get()来检索更精确的时间值,但是这个 function 没有得到广泛支持,可能无法提供预期的准确性。

Other functions are available on selected systems:其他功能在选定的系统上可用:

  • The POSIX standard definesgettimeofday() and clock_gettime() which can return precise real time with the argument CLOCK_REALTIME . POSIX 标准定义了gettimeofday()clock_gettime() ,它们可以使用参数CLOCK_REALTIME返回精确的实时时间。

  • OS/X has a more precise alternative: clock_gettime_nsec_np which returns a 64-bit value in nanosecond increments. OS/X 有一个更精确的替代方案: clock_gettime_nsec_np ,它返回一个以纳秒为增量的 64 位值。

  • Microsoft documents this for Windows. Microsoft 为 Windows 记录了这一点。

Note however that performing precise and reliable sub-microsecond benchmarks is a difficult game to say the least.但是请注意,执行精确且可靠的亚微秒级基准测试至少可以说是一项困难的游戏。

So, you should ask yourself if you really need nanosecond level.所以,你应该问问自己,你是否真的需要纳秒级。 A processor run with a clock rate in the GHz order, meaning an instruction takes in the order of nanosecond to be executed.处理器以 GHz 级的时钟频率运行,这意味着一条指令需要以纳秒级的时间执行。 This means that nanosecond accuracy is extremely complicated (if not impossible) to measure.这意味着纳秒精度的测量极其复杂(如果不是不可能的话)。

There is new timespec_get that comes with C11 that can get you time with nanosecond resolution, but it is certainly not that accurate. C11 附带的新timespec_get可以让您获得纳秒级分辨率的时间,但它肯定不是那么准确。

If your goal is to measure execution time of a function, your best option is certainly to call your function in a loop that executes it like a thousand times and you measure it with microsecond accuracy, this would certainly be closer to nanoseconds accuracy than any timer that claim to be nanoseconds.如果您的目标是测量 function 的执行时间,那么您最好的选择当然是在一个循环中调用您的 function,执行它就像一千次一样,并且您以微秒精度测量它,这肯定会比任何计时器更接近纳秒精度声称是纳秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM