Python 以亚秒级精度比较时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18169099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:01:41  来源:igfitidea点击:

Comparing times with sub-second accuracy

pythonbenchmarking

提问by user984003

How can I get the number of milliseconds since epoch?

如何获得自纪元以来的毫秒数?

Note that I want the actual milliseconds, not seconds multiplied by 1000. I am comparing times for stuff that takes less than a second and need millisecond accuracy. (I have looked at lots of answers and they all seem to have a *1000)

请注意,我想要实际的毫秒数,而不是乘以 1000 的秒数。我正在比较需要不到一秒且需要毫秒精度的东西的时间。(我看了很多答案,他们似乎都有 *1000)

I am comparing a time that I get in a POST request to the end time on the server. I just need the two times to be in the same format, whatever that is. I figured unix time would work since Javascript has a function to get that

我正在将我在 POST 请求中获得的时间与服务器上的结束时间进行比较。我只需要两次使用相同的格式,无论是什么。我认为 Unix 时间会起作用,因为 Javascript 有一个函数可以得到它

回答by duskwuff -inactive-

time.time() * 1000willgive you millisecond accuracy if possible.

time.time() * 1000如果可能的话给你误差在毫秒级。

回答by falsetru

Using datetime:

使用日期时间

>>> import datetime
>>> delta = datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)
>>> delta
datetime.timedelta(15928, 52912, 55000)
>>> delta.total_seconds()
1376232112.055
>>> delta.days, delta.seconds, delta.microseconds
(15928, 52912, 55000)

回答by Sharon J D Dorot

import datetime
time = datetime.datetime.now()
ms = time.microsecond

Returns a 6 digit number, microsecond. The last 3 digits are useless in PC since it works with ticks, which is slower than microsecond. The first 3 digits should be enough for your need.

返回一个 6 位数字,微秒。最后 3 位数字在 PC 中没有用,因为它适用于比微秒慢的滴答声。前 3 位数字应该足以满足您的需要。

回答by Michael

int(time.time() * 1000)will do what you want. time.time()generally returns a float value with double precision counting seconds since epoche, so multiplying it does no harm to the precision.

int(time.time() * 1000)会做你想做的。time.time()通常返回一个浮点值,从纪元开始以双精度计算秒数,因此乘以它不会损害精度。

Another word to the misleading answer of @kqr: time.clock()does not give you the time of the epoch. It gives you the time that the process ran on the CPU for Unix, or the time passed since the first call to the function on Windows, see the python docs.

@kqr误导性回答的另一个词:time.clock()没有给你时代的时间。它为您提供进程在 Unix 的 CPU 上运行的时间,或者自第一次调用 Windows 上的函数以来经过的时间,请参阅python 文档

Also it's true that the docs state, that time.time()is not guaranteed to give you ms precision. Though this statement is mainly ment for you to make sure not to rely on this precision on embedded or praehistoric hardware, and I'm not aware of any example, where you actually wouldn't get ms precision.

此外,文档状态确实如此,time.time()不能保证为您提供 ms 精度。尽管此声明主要是为了确保您不要依赖嵌入式或史前硬件上的这种精度,但我不知道有任何示例,您实际上无法获得 ms 精度。

回答by kqr

I see many people suggesting time.time(). While time.time()is an accurate way of measuring the actual time of day, it is not guaranteed to give you millisecond precision!From the documentation:

我看到很多人建议time.time()。虽然time.time()是测量一天中实际时间的准确方法,但不能保证为您提供毫秒精度!从文档:

Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

请注意,即使时间总是作为浮点数返回,但并非所有系统都提供比 1 秒更精确的时间。虽然此函数通常返回非递减值,但如果系统时钟在两次调用之间已回退,则它可以返回比前一次调用低的值。

This is notthe procedure you want when comparing two times! It can blow up in so many interesting ways without you being able to tell what happened. In fact, when comparing two times, you don't really need to know what time of day it is, only that the two values have the same starting point. For this, the timelibrary gives you another procedure: time.clock(). The documentation says:

不是您比较两次时想要的程序!它可以以许多有趣的方式爆炸,而您却无法说出发生了什么。事实上,当比较两个时间时,你并不需要知道现在是什么时间,只要这两个值有相同的起点即可。为此,time库为您提供了另一个程序:time.clock(). 文档说:

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

在 Unix 上,将当前处理器时间返回为以秒表示的浮点数。精度,实际上是“处理器时间”含义的定义,取决于同名 C 函数的精度,但无论如何,这是用于对 Python 或计时算法进行基准测试的函数。

在 Windows 上,此函数根据 Win32 函数 QueryPerformanceCounter() 以浮点数形式返回自第一次调用此函数以来经过的挂钟秒数。分辨率通常优于一微秒

Use time.clock().

使用time.clock().



Or if you just want to test how fast your code is running, you could make it convenient for yourself and use timeit.timeit()which does all of the measuring for you and is the de facto standard way of measuring elapsed time in code execution.

或者,如果您只是想测试您的代码运行的速度,您可以让自己方便并使用timeit.timeit()which 为您完成所有测量,并且是测量代码执行所用时间的事实上的标准方法。

回答by user136036

Python 3.7introduced time.time_ns()to finally solve this problem since time.time()as mentioned is not useful for this.

引入Python 3.7time.time_ns()最终解决了这个问题,因为time.time()如上所述对此没有用。

"returns time as an integer number of nanoseconds since the epoch."

"以自纪元以​​来的纳秒整数形式返回时间。"

https://www.python.org/dev/peps/pep-0564/
https://docs.python.org/3/library/time.html#time.time_ns

https://www.python.org/dev/peps/pep-0564/
https://docs.python.org/3/library/time.html#time.time_ns