“time python”和“time python3”之间的区别

问题描述 投票:0回答:1

使用“time python”和“time python3”检查执行时间有什么区别?

当我使用

time python test.py
时,我得到:

zsh: command not found: python
python test.py  0.00s user 0.00s system 56% cpu 0.003 total

而当我使用

time python3 test.py
时,我得到:

python3 test.py  0.05s user 0.01s system 90% cpu 0.064 total
python python-3.x time
1个回答
0
投票

time python 和 time python3 之间的区别在于用于运行脚本的解释器。这是一个细分:

时间蟒蛇: 尝试使用系统上默认的 Python 解释器运行脚本。 这可能会出现问题,因为: 可能会安装多个 Python 版本:如果 Python 2 和 3 都可用,Python 可能会默认使用较旧、速度较慢的 Python 2,从而导致计时结果不准确。 默认行为可能会有所不同:根据您的系统配置,默认解释器可能不是您所期望的。 时间Python3: 显式指定 Python 3 解释器,确保您的脚本以所需的版本运行。 这保证: 准确计时:您正在使用正确的 Python 版本来测量脚本的性能。 改进的性能:与 Python 2 相比,Python 3 提供了显着的性能改进,因此您可能会看到更快的执行时间。 在您的示例中,使用 time python 和 time python3 观察到的执行时间差异可能反映了两个不同 Python 版本的使用。 Python 2 明显变慢解释了 python 的执行时间更长。

因此,对于专业环境和准确计时,始终建议使用 time python3 来显式指定 Python 3 解释器并确保一致的、有代表性的结果。

© www.soinside.com 2019 - 2024. All rights reserved.