您的位置:首页 > 运维架构 > Linux

Measure time in Linux - getrusage vs clock_gettime vs clock vs gettimeofday

2016-10-08 16:57 337 查看
http://stackoverflow.com/questions/12392278/measure-time-in-linux-getrusage-vs-clock-gettime-vs-clock-vs-gettimeofday

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:

What time is measured by the clock? (real, user, system, or, hopefully not, wall-clock?)

What is the precision of the clock? (s, ms, ?s, or faster?)

After how much time does the clock wrap around? Or is there some mechanism to avoid this?

Is the clock monotonic, or will it change with changes in the system time (via NTP, time zone, daylight savings time, by the user, etc.)?

How do the above vary between implementations?

Is the specific function obsolete, non standard, etc.?

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

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

time() returns the wall-clock time from the OS, with precision in seconds.

clock() seems to return the sum of user and system time. At one time this was supposed to be the CPU time in cycles, but modern standards 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).

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.

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

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.

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 


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

阅读(245) | 评论(0) | 转发(0) |

0
上一篇:指针,变量从函数取值

下一篇:串口通信 RS232 422 485

相关热门文章
SHTML是什么_SSI有什么用...

卡尔曼滤波的原理说明...

查看linux中某个端口(port)...

关于java中的“错误:找不到或...

shell中字符串操作

linux dhcp peizhi roc

关于Unix文件的软链接

求教这个命令什么意思,我是新...

sed -e "/grep/d" 是什么意思...

谁能够帮我解决LINUX 2.6 10...

给主人留下些什么吧!~~

评论热议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: