用于测量代码运行时间的时间和日期时间模块之间的差异
Discrepancy between time and datetime modules for measuring code runtime
我正在使用时间模块测量函数的响应时间。时间模块应该将秒数输出为浮点数,所以我保存了一个开始时间值 (time.clock()) 并在结束时再次读取,并将差值用作运行时间。在观察结果时,我们注意到运行时间似乎很长——例如,似乎需要不到 2 秒的时间打印为 3-and-change。基于感知到的问题,我决定使用日期时间模块仔细检查结果。并排打印两个显示时间模块值几乎是日期时间值的两倍。
有人知道为什么会这样吗?
这是我的代码:
for datum in data:
start = datetime.datetime.now()
startts = time.clock()
check = test_func(datum)
runtime = datetime.datetime.now() - start
runts = time.clock() - startts
print(check, "Time required:", runtime, "or", runts)
我的一些结果:
XYZ Time required: 0:00:01.985303 or 3.7836029999999994
XYZ Time required: 0:00:01.476289 or 3.3465039999999817
XYZ Time required: 0:00:01.454407 or 3.7140109999999993
XYZ Time required: 0:00:01.550416 or 3.860824000000008
我假设这种问题以前会被注意到,我只是在我的实现中遗漏了一些基本的东西。有人可以告诉我吗?
看起来 time.clock() 自版本 3.3 以来已被弃用
也许 this 会有帮助?
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.
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.
Deprecated since version 3.3: The behaviour of this function depends
on the platform: use perf_counter() or process_time() instead,
depending on your requirements, to have a well defined behaviour.
我们发现了问题。我正在测试的 test_func 正在使用多线程进程。我都不知道,也不知道这是一个问题。
时间模块使用处理器时间 (https://docs.python.org/3.6/library/time.html), while the datetime module uses wall clock time (https://docs.python.org/3.6/library/datetime.html)。使用日期时间戳中的差异告诉我实际经过了多少时间,并且对于我们的目的是相关信息。
我希望这对以后的其他人有所帮助!
我正在使用时间模块测量函数的响应时间。时间模块应该将秒数输出为浮点数,所以我保存了一个开始时间值 (time.clock()) 并在结束时再次读取,并将差值用作运行时间。在观察结果时,我们注意到运行时间似乎很长——例如,似乎需要不到 2 秒的时间打印为 3-and-change。基于感知到的问题,我决定使用日期时间模块仔细检查结果。并排打印两个显示时间模块值几乎是日期时间值的两倍。
有人知道为什么会这样吗?
这是我的代码:
for datum in data:
start = datetime.datetime.now()
startts = time.clock()
check = test_func(datum)
runtime = datetime.datetime.now() - start
runts = time.clock() - startts
print(check, "Time required:", runtime, "or", runts)
我的一些结果:
XYZ Time required: 0:00:01.985303 or 3.7836029999999994
XYZ Time required: 0:00:01.476289 or 3.3465039999999817
XYZ Time required: 0:00:01.454407 or 3.7140109999999993
XYZ Time required: 0:00:01.550416 or 3.860824000000008
我假设这种问题以前会被注意到,我只是在我的实现中遗漏了一些基本的东西。有人可以告诉我吗?
看起来 time.clock() 自版本 3.3 以来已被弃用
也许 this 会有帮助?
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.
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.
Deprecated since version 3.3: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.
我们发现了问题。我正在测试的 test_func 正在使用多线程进程。我都不知道,也不知道这是一个问题。
时间模块使用处理器时间 (https://docs.python.org/3.6/library/time.html), while the datetime module uses wall clock time (https://docs.python.org/3.6/library/datetime.html)。使用日期时间戳中的差异告诉我实际经过了多少时间,并且对于我们的目的是相关信息。
我希望这对以后的其他人有所帮助!