在Python中测量经过时间 [英] Measuring elapsed time in python

查看:105
本文介绍了在Python中测量经过时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法/模块来正确地 测量python中的经过时间?我知道我可以简单地调用 time.time()两次并取差值,但是如果更改系统时间,将会产生错误的结果。当然,这种情况很少发生,但这确实表明我正在测量错误的东西。

Is there a simple way / module to correctly measure the elapsed time in python? I know that I can simply call time.time() twice and take the difference, but that will yield wrong results if the system time is changed. Granted, that doesn't happen very often, but it does indicate that I'm measuring the wrong thing.

使用 time.time() 来衡量持续时间是您想像中难以置信的环岛。您可以采用两个绝对时间测量值的差,这两个绝对时间测量值由持续时间测量值(由计时器执行)和已知的绝对时间(手动设置或通过ntp设置)构成,您根本不感兴趣。

Using time.time() to measure durations is incredibly roundabout when you think about it. You take the difference of two absolute time measurements which are in turn constructed from duration measurements (performed by timers) and known absolute times (set manually or via ntp), that you aren't interested in at all.

那么,有没有一种方法可以直接查询计时器时间?我以为它可以表示为毫秒或微秒值,而没有有意义的绝对表示形式(因此不需要随系统时间进行调整)。环顾四周,似乎这正是 System.nanoTime()在Java中所做的事情,但是我没有找到相应的Python函数,即使它应该这样做(硬件-技术上来说)比 time.time()更容易提供。

So, is there a way to query this "timer time" directly? I'd imagine that it can be represented as a millisecond or microsecond value that has no meaningful absolute representation (and thus doesn't need to be adjusted with system time). Looking around a bit it seems that this is exactly what System.nanoTime() does in Java, but I did not find a corresponding Python function, even though it should (hardware-technically) be easier to provide than time.time().

编辑:为避免混淆并解决答案下图:这与DST更改无关,我也不希望CPU时间-我希望经过的物理时间。它不需要非常精细,甚至不需要特别准确。它只是不应该给我负面的持续时间,也不给我持续数个数量级(超过粒度)的持续时间,只是因为有人决定将系统时钟设置为其他值。这是Python文档对 time.time()的评价:

To avoid confusion and address the answers below: This is not about DST changes, and I don't want CPU time either - I want elapsed physical time. It doesn't need to be very fine-grained, and not even particularly accurate. It just shouldn't give me negative durations, or durations which are off by several orders of magnitude (above the granularity), just because someone decided to set the system clock to a different value. Here's what the Python docs say about 'time.time()':

虽然此函数通常返回非递减值,但如果在两个调用之间重新设置了系统时钟,则它可以返回比上一个调用更低的值

这正是我要避免的事情,因为它可能导致奇怪的事情,例如在时间计算中出现负值。我目前可以解决此问题,但我相信在可行的情况下学习使用适当的解决方案是一个好主意,因为纠结一天有一天会再来咬你。

This is exactly what I want to avoid, since it can lead to strange things like negative values in time calculations. I can work around this at the moment, but I believe it is a good idea to learn using the proper solutions where feasible, since the kludges will come back to bite you one day.

Edit2:一些研究表明,您可以使用GetTickCount64()在Windows中获得我想要的与系统时间无关的度量,而在Linux下,您可以在times()的返回值中获得它。但是,我仍然找不到在Python中提供此功能的模块。

Some research shows that you can get a system time independent measurement like I want in Windows by using GetTickCount64(), under Linux you can get it in the return value of times(). However, I still can't find a module which provides this functionality in Python.

推荐答案

您似乎正在寻找的是单调计时器单调时间参考不会跳转或后退。

What you seem to be looking for is a monotonic timer. A monotonic time reference does not jump or go backwards.

有已经进行了多次尝试,以基于其操作系统参考为Python实现跨平台的单时钟。 (Windows,POSIX和BSD完全不同)请参见此SO帖子

There have been several attempts to implement a cross platform monotomic clock for Python based on the OS reference of it. (Windows, POSIX and BSD are quite different) See the discussions and some of the attempts at monotonic time in this SO post.

通常,您只能使用os.times():

Mostly, you can just use os.times():


os.times()

os.times()

返回一个5元的浮点数,表示
的累积(处理器或其他)时间,在秒。这些项目是:
用户时间,系统时间,孩子们的用户时间,孩子们的系统时间,
和自过去某个固定点以来经过的实时时间(按此顺序)。
参见Unix手册页times(2)或相应的Windows
Platform API文档。在Windows上,只有前两个项目填充了
,其他的则为零。

Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children’s user time, children’s system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. On Windows, only the first two items are filled, the others are zero.

可用性:Unix,Windows

Availability: Unix, Windows

但是,这不能满足Windows所需的经过时间(第五个元组)。

But that does not fill in the needed elapsed real time (the fifth tuple) on Windows.

如果需要Windows支持,请考虑 ctypes ,您可以直接调用GetTickCount64(),就像在此食谱

If you need Windows support, consider ctypes and you can call GetTickCount64() directly, as has been done in this recipe.

这篇关于在Python中测量经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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