测量的Linux时间 - 时间与时钟VS的getrusage VS clock_gettime VS gettimeofday的VS timespec_get? [英] Measure time in Linux - time vs clock vs getrusage vs clock_gettime vs gettimeofday vs timespec_get?

查看:410
本文介绍了测量的Linux时间 - 时间与时钟VS的getrusage VS clock_gettime VS gettimeofday的VS timespec_get?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中计时功能,时间时钟 的getrusage clock_gettime gettimeofday的 timespec_get ,我想清楚地了解它们是如何实现的,为了知道在哪个情况下,我必须使用他们什么是他们的返回值。

Among the timing functions, time, clock getrusage, clock_gettime, gettimeofday and timespec_get, I want to understand clearly how they are implemented and what are their return values in order to know in which situation I have to use them.

首先,我们需要进行分类的功能返回挂钟值比较函数返回的进程或线程值 gettimeofday的返回挂钟值, clock_gettime 返回挂钟值进程或线程视时钟参数传递给它的值。 的getrusage 时钟返回过程值。

First we need to classify functions returning wall-clock values compare to functions returning process or threads values. gettimeofday returns wall-clock value, clock_gettime returns wall-clock value or process or threads values depending on the Clock parameter passed to it. getrusage and clock return process values.

然后,第二个问题关于这些功能的执行情况,并作为结果,其精确度。它的硬件或软件机制做这些功能的使用。

Then the second question regards the implementation of these functions and as a consequence, their accuracy. Which hardware or software mechanism does these functions use.

似乎的getrusage 仅使用内核蜱(通常为1ms长),并作为结果不能比毫秒更准确。这样对吗?
那么 getimeofday 功能似乎使用可用的最准确的底层硬件。其结果是它的精度通常是微秒(不能更因为API)的最近的硬件。
什么时钟,该男子页关于逼近说,这是什么意思?
关于 clock_gettime 什么,该API是在纳秒,这是否意味着它能够成为,如果底层硬件允许,那么准确?有关单调性是什么?

It seems that getrusage uses only the kernel tick (usually 1ms long) and as a consequence can't be more accurate than the ms. Is it right? Then the getimeofday function seems to use the most accurate underlying hardware available. As a consequence its accuracy is usually the microsecond (can't be more because of the API) on recent hardware. What about clock, the man page speak about "approximation", what does it mean? What about clock_gettime, the API is in nanosecond, does it means that it's able to be so accurate if underlying hardware allows it? What about monotonicity?

还有没有其他的功能呢?

Are there any other functions?

推荐答案

的问题是,有C和C ++提供了一些不同的时间函数,其中一些在实现之间的行为变化。还有一个漂浮大量的半答案。它们的属性编译的时钟功能列表一起将正确地回答这个问题。对于开始,让我们问什么相关的属性是我们正在寻找的。看着你的帖子,我建议:

The problem is that there are several different time functions available in C and C++, and some of them vary in behavior between implementations. There are also a lot of half-answers floating around. Compiling a list of clock functions together with their properties would answer the question properly. For starts let's ask what the relevant properties are that we're looking for. Looking at your post, I suggest:


  • 什么时间由时钟测量? (实际时间,用户,系统,或者希望不​​是,挂钟?)

  • 什么是时钟的precision? (S,MS,微秒,或更快?)

  • 后多少时间时钟环绕?或者是有一些机制来避免这种情况?

  • 是时钟单调,还是会在系统时间的变化而变化(通过NTP,时区,夏令时,用户等)?

  • 如何做到上面的实现之间有什么不同?

  • 是特定的功能过时的,非标准的,等等?

在启动列表之前,我想指出的是挂钟时间很少使用权的时间,而它与时区变化,夏令时的变化而变化,或者挂钟由NTP同步。这一切都不如果您使用的时间来安排事件或基准性能都不错。这只是什么名字说真的很不错,在墙上(或台式机)的时钟。

Before starting the list, I'd like to point out that wall-clock time is rarely the right time to use, whereas it changes with time zone changes, daylight savings time changes, or if the wall clock is synchronized by NTP. None of these things are good if you're using the time to schedule events or to benchmark performance. It's only really good for what the name says, a clock on the wall (or desktop).

下面是我到目前为止已经找到了在Linux和OS X时钟:

Here's what I've found so far for clocks in Linux and OS X:


  • 时间() 返回挂钟从OS,以秒precision时间。

  • 时钟() 似乎返回总和的用户和系统时间。这是present在C89和更高版本。有一段时间,这应该是在周期的CPU时间,但现代的标准像POSIX 要求CLOCKS_PER_SEC是1000000给人的1μs的最大可能precision。我的系统上的precision的确是1微秒。这个时钟环绕,一旦顶出(这通常发生〜2 ^ 32蜱,这是不是很长,1 MHz的时钟后)。 男人时钟表示,因为glibc 2.18它是在Linux中 clock_gettime(CLOCK_PROCESS_CPUTIME_ID,...)实施。

  • clock_gettime(CLOCK_MONOTONIC,...) 提供纳秒的分辨率,是单调的。我相信秒和纳秒'被分别储存,每个在32位计数器。因此,会后许多十几年的运行时间发生的任何回绕。这看起来像一个很好的时钟,但不幸的是尚未公布在OS X POSIX 7 描述 CLOCK_MONOTONIC 作为一个可选扩展

  • 的getrusage() 竟然是我的情况最好的选择。它单独报告用户和系统时间,并且不绕回。我的系统上的precision为1μs,但我也测试了它在Linux系统(红帽4.1.2-48与GCC 4.1.2)上,并在那里的precision仅为1毫秒。

  • 函数gettimeofday() 返回挂钟时间(标称)μs的precision。在我的系统这个时钟似乎有微秒precision,但是这不能保证,因为系统的分辨率时钟是依赖于硬件。 POSIX.1-2008 。 应用程序应该使用 clock_gettime()函数代替过时的函数gettimeofday()功能,所以你应该远离从中。 Linux的x86和实现它作为一个系统调用

  • <$c$c>mach_absolute_time()是在OS X上的非常高的分辨率(NS)时序选项在我的系统,这确实给ns的分辨率。原则上这个时钟环绕,但它是存储使用的64位无符号整数纳秒,所以围绕包装不应该是在实践中的一个问题。便携性是值得怀疑的。

  • I在编译时写道基于一个混合功能这个片段使用clock_gettime在Linux上编译的时候,还是马赫计时器在OS X上,以获得在Linux和OS X NS precision。

  • time() returns the wall-clock time from the OS, with precision in seconds.
  • clock() seems to return the sum of user and system time. It is present in C89 and later. At one time this was supposed to be the CPU time in cycles, but modern standards like POSIX require CLOCKS_PER_SEC to be 1000000, giving a maximum possible precision of 1 µs. The precision on my system is indeed 1 µs. This clock wraps around once it tops out (this typically happens after ~2^32 ticks, which is not very long for a 1 MHz clock). man clock says that since glibc 2.18 it is implemented with clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) in Linux.
  • clock_gettime(CLOCK_MONOTONIC, ...) provides nanosecond resolution, is monotonic. I believe the 'seconds' and 'nanoseconds' are stored separately, each in 32-bit counters. Thus, any wrap-around would occur after many dozen years of uptime. This looks like a very good clock, but unfortunately it isn't yet available on OS X. POSIX 7 describes CLOCK_MONOTONIC as an optional extension.
  • getrusage() turned out to be the best choice for my situation. It reports the user and system times separately and does not wrap around. The precision on my system is 1 µs, but I also tested it on a Linux system (Red Hat 4.1.2-48 with GCC 4.1.2) and there the precision was only 1 ms.
  • gettimeofday() returns the wall-clock time with (nominally) µs precision. On my system this clock does seem to have µs precision, but this is not guaranteed, because "the resolution of the system clock is hardware dependent". POSIX.1-2008 says that. "Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function", so you should stay away from it. Linux x86 and implements it as a system call.
  • mach_absolute_time() is an option for very high resolution (ns) timing on OS X. On my system, this does indeed give ns resolution. In principle this clock wraps around, however it is storing ns using a 64-bit unsigned integer, so the wrapping around shouldn't be an issue in practice. Portability is questionable.
  • I wrote a hybrid function based on this snippet that uses clock_gettime when compiled on Linux, or a Mach timer when compiled on OS X, in order to get ns precision on both Linux and OS X.

在Linux和OS X上述所有的存在,除非另有说明。在上面的我的系统是一个运行苹果OS X 10.8.3从MacPorts的GCC 4.7.2。

All of the above exist in both Linux and OS X except where otherwise specified. "My system" in the above is an Apple running OS X 10.8.3 with GCC 4.7.2 from MacPorts.

最后,这里是我除了上面的链接发现的有用的参考列表:

Finally, here is a list of references that I found helpful in addition to the links above:

  • http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time
  • How to measure the ACTUAL execution time of a C program under Linux?
  • http://digitalsandwich.com/archives/27-benchmarking-misconceptions-microtime-vs-getrusage.html
  • http://www.unix.com/hp-ux/38937-getrusage.html

这篇关于测量的Linux时间 - 时间与时钟VS的getrusage VS clock_gettime VS gettimeofday的VS timespec_get?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆