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

查看:27
本文介绍了在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:一些研究表明,在 Windows 中使用 GetTickCount64() 可以获得像我想要的系统时间无关的测量值,在 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 的 OS 参考为 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()

返回一个 5 元组的浮点数表示累积(处理器或其他)时间,以秒为单位.项目是:用户时间,系统时间,儿童用户时间,儿童系统时间,并按照该顺序从过去的某个固定点开始实时流逝.请参阅 Unix 手册页 times(2) 或相应的 Windows平台 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天全站免登陆