【发布时间】:2019-03-05 17:26:29
【问题描述】:
我只在我的 iMac(macOS Mojave,版本 10.14.3)上看到了这个问题。我的 Windows 和 Linux (CentOS) 计算机没有这样的问题。有 iMac 的人可以证实这一点吗?
当我使用适用于 Mac OS 的最新 Python 3.7 版本运行时,process_time 时间(CPU 时间)是单线程程序中 perf_counter(挂钟)的两倍:
$ python3.7 test.py
Python ('v3.7.2:9a3ffc0492', 'Dec 24 2018 02:44:43') Clang 6.0 (clang-600.0.57)
CPU 时间:15.925813999999999 挂钟:7.970086342 距离:750
我没有看到 Python 3.5 存在同样的问题:
$ python3.5 test.py
Python ('v3.5.1:37a07cee5969', 'Dec 5 2015 21:12:44') GCC 4.2.1(Apple Inc. build 5666)(点 3)
CPU 时间:8.09766 挂钟:8.108357406000323 距离:750
这是 Python 3.7 中的错误还是我对 process_time 不了解?
这是我运行“test.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)
代码在这里
【问题讨论】: