运行时编译:这怎么可能不会影响性能呢?
我听说某些类型的运行时编译不会影响性能。 Python 官方文档声称运行 .pyc
文件并不比 .py
快。 (我现在找不到链接。)这怎么可能?从 .py
生成字节码不需要时间吗?
像 PHP 这样的语言呢?是否有必要在每次执行时都编译它们?你为什么想要这个?如果同一个脚本运行多次似乎是浪费时间。
I've heard that some types of runtime compilation aren't a performance hit. The official Python documentation asserts that running a .pyc
file is no faster than .py
. (I can't find the link right now.) How is this possible? Doesn't it take time to generate bytecode from the .py
?
What about languages like PHP? Isn't it necessary to compile them on every execution? Why would you want this? It seems like a waste of time if the same script will be run many times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有您正在阅读的实际文档的上下文,就不可能肯定地说,但我认为您可能误解了“性能”的含义。
如果应用程序必须在启动时编译为字节码,则应用程序启动速度会变慢,但它的运行速度不会变慢。如果没有限定条件,有人谈论“性能”时几乎绝不会指启动时间。
Without the context of the actual document you were reading, it's impossible to say for sure, but I think you might be misunderstanding what is meant by "performance".
The application starts slower if it has to be compiled to bytecode on startup, but it doesn't run slower. Without qualification, someone talking about "performance" is almost never referring to start-up time.
Python 检查
.pyc
文件,当该文件不存在或已过期时,它将从.py
文件重新创建字节码。如果文件系统不可写,则此步骤仍会在程序运行之前在 RAM 中发生。
因此,Python 在运行程序时永远不会逐行解释代码。
结果是,如果
.pyc
文件存在并且是最新的,您的程序将更快地开始运行,但执行速度将相同Python checks for a
.pyc
file and when it does not exist, or if it is out of date it will recreate the bytecode from the.py
file.If the filesystem is not writeable, this step still happens in RAM before the program runs.
So Python is never interpreting code line by line as it runs the program.
The upshot is that if the
.pyc
file exists and is current, you program will start running a tiny bit sooner, but the execution speed will be the same