python中加载时间和运行时间的区别?
我引用了Python文档的一部分: “从 .pyc 或 .pyo 文件读取程序时,其运行速度并不比从 .py 文件读取时快;.pyc 或 .pyo 文件唯一更快的是它们的速度已加载。”
我不明白它说它不影响运行时间但影响加载时间是什么意思?有人可以解释一下吗?我可以完全理解吗?
I am quoting a part of Python documentation:
"A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded."
I don't understand what does it mean when it says it doesn't affect the running time but the loading time? Could someone please explain it a little deep that I can understand completely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您导入模块
test.py
时,Python 必须读取源代码并将其转换为 Python 可以执行的字节码。这需要时间,但 Python 会将其存储在 test.pyc 中。该字节码是将您的代码分解为能够直接在 CPython 虚拟机上运行的更简单术语的结果。如果加载
test.pyc
,Python 不需要在运行前将源代码编译为字节码,因此启动时间会稍微少一些。如果两次导入模块
test.py
且未修改它或删除生成的test.pyc
,Python 会检查test.pyc
是否存在并加载它 - 因此性能优势是自动的。When you import a module
test.py
, Python must read the source and convert it into the bytecode Python can execute. This takes time, but Python will store this intest.pyc
. This Bytecode is the result of breaking your code down into simpler terms able to run directly on the CPython Virtual Machine.If you load
test.pyc
, Python doesn't need to compile your source into bytecode before running, so it takes slightly less time to start.If you import the module
test.py
twice without modifying it or deleting the generatedtest.pyc
, Python checks for the existence oftest.pyc
and loads it instead - so the performance benefit is automatic.将您编写的 Python 代码转换为计算机可以理解的指令有两个步骤:
编译步骤。原始 Python 代码转换为 Python 字节码。该字节码将被任何操作系统、任何硬件上的 Python 解释器识别。这是存储在 .pyo 或 .pyc 文件中的内容。
解释步骤。 Python 解释器(或者如果您更喜欢 Python 虚拟机)会解释字节码并向计算机发送低级指令。这些低级指令在 Linux 和 Windows 之间,或者在 Intel 芯片和 AMD 等之间会有所不同,因此必须有人为 Python 可以运行的每种类型的系统编写不同的解释器。
当您从 .pyc 文件运行代码时,步骤 1 已经完成,因此执行直接进入步骤 2。但是步骤 2 的运行速度与在运行之前立即编译它的运行速度一样快。编译步骤是否会显着减慢代码速度取决于程序的功能。您应该进行试验,看看等待代码编译的差异有多大,但如果您编写简短的脚本,则差异可能不会明显。
There are two steps in converting the Python code you write to instructions the computer can understand:
A compile step. The raw Python code is converted to Python bytecode. This bytecode will be recognised by a Python interpreter on any operating system, on any hardware. This is what is stored in a .pyo or .pyc file.
An interpretation step. The Python interpreter, or if you prefer the Python virtual machine, interprets the bytecode and sends low-level instructions to the computer. These low level instructions will be different between Linux and Windows, or between an Intel chip and an AMD, etc, so someone has to write a different interpreter for each type of system that Python can be run on.
When you run code from a .pyc file, step 1 has already been completed, so the execution goes straight to step 2. But step 2 runs just as fast as it would run if you compiled it immediately before running it. Whether the compile step slows down your code significantly depends on what your program does. You should experiment to see how big a difference waiting for your code to compile takes, but if you are writing short scripts the difference will probably be unnoticeable.