我仅在我的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)(dot 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)
这里的代码
最佳答案
是的,这是来自https://www.python.org下载的macOS的Python 3.7和3.8二进制发行版中的错误:
$ python3.8 test.py
Python version : 3.8.0a2
build : ('v3.8.0a2:23f4589b4b', 'Feb 25 2019 10:59:08')
compiler: Clang 6.0 (clang-600.0.57)
CPU Time: 16.005979999999997 Wall Clock: 8.014987319 Distance: 750
我从gitHub获得了源代码,并在Mac上使用最新的编译器对其进行了编译,问题消失了:
$ ./python.exe test.py
Python version : 3.8.0a2+
build : ('heads/master:a9df651eb4', 'Mar 5 2019 17:21:48')
compiler: Clang 10.0.0 (clang-1000.11.45.5)
CPU Time: 7.2123870000000005 Wall Clock: 7.21903976 Distance: 750
修改了“ test.py”程序以正确报告Python版本:
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 version :", platform.python_version())
print(" build :", platform.python_build())
print(" compiler:", 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)