我只在iMac上看到这个问题(MacOSMojave,10.14.3版)。我的Windows和Linux(CentOS)计算机没有这个问题。iMac的人能确认一下吗?在
当我使用最新的适用于Mac OS的Python 3.7版本运行时,在单线程程序中,进程时间时间(CPU时间)比性能计数器大两倍:$ python3.7 test.py
Python ('v3.7.2:9a3ffc0492', 'Dec 24 2018 02:44:43') Clang 6.0 (clang-600.0.57)
CPU time: 15.925813999999999 Wall clock: 7.970086342 Distance: 750
我看不出Python3.5有同样的问题:$ python3.5 test.py
Python ('v3.5.1:37a07cee5969', 'Dec 5 2015 21:12:44') GCC 4.2.1 (Apple Inc. build 5666) (dot 3)
CPU time: 8.09766 Wall clock: 8.108357406000323 Distance: 750
是Python3.7中的一个bug,还是我对进程时间有些不理解?在
这是我运行的代码'测试.py':import time
import sys
import platform
def distance(a, b):
if a == b:
return 0
d = sys.maxsize
for i, c in enumerate(a):
d = min(d, ord(c) + distance(a[:i]+a[i+1:], b))
for i, c in enumerate(b):
d = min(d, ord(c) + distance(a, b[:i]+b[i+1:]))
return d
print("Python", platform.python_build(), platform.python_compiler())
cpu = time.process_time()
clock = time.perf_counter()
d = distance("12345", "abcde")
clock = time.perf_counter() - clock
cpu = time.process_time() - cpu
print("CPU Time:", cpu, "Wall Clock:", clock, " Distance:", d)
代码在这里