在 python 中测量长时间间隔
Measuring long time intervals in python
在 Python 3.x 中搜索了不同的时间测量方法后,我决定使用 perf_counter()来自 time 模块的函数。当我用一个需要几秒钟到 运行 的函数对其进行测试时,它工作正常,但我想从中提取计时结果的代码需要数十小时到 运行.
今天我得到了结果,令我惊讶的是计时结果是几分之一秒的数量级,这是荒谬的。因此,我来请教大家为什么会出现这种情况,以及如何在Python 3.x.
中准确测量长的时间间隔
使用代码如下:
optimization_times = []
for i in range(30):
# Search parameters on train set
print("Round %d" % i)
time_start = time.perf_counter()
solution = metaheuristic.optimize()
time_end = time.perf_counter()
# Keep time spent
optimization_times.append(time_end - time_start)
操作系统:Ubuntu18.04
提前致谢。
time.perf_counter()
→ float
Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
--
"what is a fractional second?"
Fractional second is the part of the time that is not an integer. So if you have a time like 12345678.9 the fractional second is 0.9
(Basile Starynkevitch)
可能的解决方案是timeit
:
import timeit
import time
def func():
time.sleep(0.05)
timeit.timeit(lambda: func(), number=10)
输出:
0.528169795999986
你看到了环绕。
来自docs:
... a clock with the highest available resolution to measure a short duration.
如果你的间隔是几十个小时,你应该坚持time.time()
,
它有足够的分辨率满足您的需求。
如果你认为你真的需要一个疯狂的数字精度,
然后结合 time.time() 和 perf_counter(),
旁边还有一个程序
定期记录这两个值,以便您可以
将 perf_counter 偏移同步到全球时间。
这样的日志对应
使您的 48 位计数器更宽一些,
它可以让您观察每个环绕事件。
在 Python 3.x 中搜索了不同的时间测量方法后,我决定使用 perf_counter()来自 time 模块的函数。当我用一个需要几秒钟到 运行 的函数对其进行测试时,它工作正常,但我想从中提取计时结果的代码需要数十小时到 运行.
今天我得到了结果,令我惊讶的是计时结果是几分之一秒的数量级,这是荒谬的。因此,我来请教大家为什么会出现这种情况,以及如何在Python 3.x.
中准确测量长的时间间隔使用代码如下:
optimization_times = []
for i in range(30):
# Search parameters on train set
print("Round %d" % i)
time_start = time.perf_counter()
solution = metaheuristic.optimize()
time_end = time.perf_counter()
# Keep time spent
optimization_times.append(time_end - time_start)
操作系统:Ubuntu18.04
提前致谢。
time.perf_counter()
→ floatReturn the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
--
"what is a fractional second?"
Fractional second is the part of the time that is not an integer. So if you have a time like 12345678.9 the fractional second is 0.9
(Basile Starynkevitch)
可能的解决方案是timeit
:
import timeit
import time
def func():
time.sleep(0.05)
timeit.timeit(lambda: func(), number=10)
输出:
0.528169795999986
你看到了环绕。
来自docs:
... a clock with the highest available resolution to measure a short duration.
如果你的间隔是几十个小时,你应该坚持time.time()
,
它有足够的分辨率满足您的需求。
如果你认为你真的需要一个疯狂的数字精度, 然后结合 time.time() 和 perf_counter(), 旁边还有一个程序 定期记录这两个值,以便您可以 将 perf_counter 偏移同步到全球时间。 这样的日志对应 使您的 48 位计数器更宽一些, 它可以让您观察每个环绕事件。