首页 > 解决方案 > Python 脚本第一次执行需要几秒钟

问题描述

我有一台带有 High Sierra 版本 10.13.6 的新 Mac。默认安装的python有版本:

➜  ~ python --version
Python 2.7.10

我安装了python 3:

➜  ~ brew install python

所以我得到:

➜  ~ python3 --version
Python 3.7.0

使用这两个版本的python,当我运行一个非常简单的脚本时: print("this is the text")第一次执行很慢:

➜  ~ time python3 test.py
this is the text
python3 test.py  0.02s user 0.02s system 1% cpu 2.945 total

随后的执行速度要快得多:

➜  ~ time python3 test.py
this is the text
python3 test.py  0.03s user 0.02s system 70% cpu 0.071 total

每当我更改脚本中的某些内容并重新运行它时,第一次执行非常慢。这发生在两个版本的 python 中。我在 ubuntu 上尝试了相同的步骤,但没有证据表明这个问题。

我错过了什么?我怎样才能使第一次执行在合理的时间内运行?

标签: pythonpython-3.xpython-2.7

解决方案


推荐阅读