【问题标题】:Python 3.5 vs 3.7 process_timePython 3.5 与 3.7 进程时间
【发布时间】: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)

代码在这里

【问题讨论】:

    标签: python macos time process


    【解决方案1】:

    是的,这是来自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)
    

    【讨论】:

    • 该错误是 3.7.0 中引入的回归,每当 Python 在 macOS 10.11 或更早版本上构建时都会出现。修复现在可用,并将在 Python 3.7.5 和 3.8.0b4 中发布。有关详细信息,请参阅Issue36205
    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2019-07-27
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多