为什么 python 在解释之前将源代码编译为字节码?

发布于 2024-07-22 13:20:01 字数 58 浏览 9 评论 0原文

为什么 python 在解释之前将源代码编译为字节码?

为什么不直接从源头解读呢?

Why python compile the source to bytecode before interpreting?

Why not interpret from the source directly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝梦月影 2024-07-29 13:20:01

几乎没有解释器真正直接逐行解释代码——效率太低了。 几乎所有解释器都使用一些可以轻松执行的中间表示。 此外,还可以对此中间代码执行小的优化。

Python 还存储了这段代码,这对于下次执行这段代码有一个巨大的优势:Python 不再需要解析代码; 解析是编译过程中最慢的部分。 因此,字节码表示大大减少了执行开销。

Nearly no interpreter really interprets code directly, line by line – it's simply too inefficient. Almost all interpreters use some intermediate representation which can be executed easily. Also, small optimizations can be performed on this intermediate code.

Python furthermore stores this code which has a huge advantage for the next time this code gets executed: Python doesn't have to parse the code anymore; parsing is the slowest part in the compile process. Thus, a bytecode representation reduces execution overhead quite substantially.

梦境 2024-07-29 13:20:01

因为您可以编译为 .pyc 一次并对其进行多次解释。

因此,如果您多次运行脚本,则只需解析一次源代码的开销。

Because you can compile to a .pyc once and interpret from it many times.

So if you're running a script many times you only have the overhead of parsing the source code once.

ˉ厌 2024-07-29 13:20:01

因为直接从字节码解释速度更快。 一方面,它避免了进行词法分析的需要。

Because interpretting from bytecode directly is faster. It avoids the need to do lexing, for one thing.

孤独难免 2024-07-29 13:20:01

一遍又一遍地重新词法分析和解析源代码,而不是只做一次(通常是在第一次导入时),显然是一种愚蠢且毫无意义的浪费。

Re-lexing and parsing the source code over and over, rather than doing it just once (most often on the first import), would obviously be a silly and pointless waste of effort.

沧桑㈠ 2024-07-29 13:20:01

尽管它有一个小的效率方面(您可以将字节码存储在磁盘或内存中),但它主要是工程方面的:它允许您将解析与解释分开。 解析器通常是令人讨厌的生物,充满了边缘情况,并且必须遵守深奥的规则,例如使用适量的前瞻和解决移位归约问题。 相比之下,解释非常简单:它只是一个使用字节码操作码的大 switch 语句。

Although there is a small efficiency aspect to it (you can store the bytecode on disk or in memory), its mostly engineering: it allows you separate parsing from interpreting. Parsers can often be nasty creatures, full of edge-cases and having to conform to esoteric rules like using just the right amount of lookahead and resolving shift-reduce problems. By contrast, interpreting is really simple: its just a big switch statement using the bytecode's opcode.

同尘 2024-07-29 13:20:01

我非常怀疑原因是性能,尽管这是一个很好的副作用。 我想说,很自然地认为围绕某种高级汇编语言构建的虚拟机比在某些源代码字符串中查找和替换文本更实用。

编辑:

好吧,显然,谁对我的帖子投了 -1 票而没有留下合理的评论来解释,对虚拟机(运行时环境)知之甚少。

http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Lars-Bak-Inside-V8-A-Javascript-Virtual-Machine /

I doubt very much that the reason is performance, albeit be it a nice side effect. I would say that it's only natural to think a VM built around some high-level assembly language would be more practical than to find and replace text in some source code string.

Edit:

Okay, clearly, who ever put a -1 vote on my post without leaving a reasonable comment to explain knows very little about virtual machines (run-time environments).

http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Lars-Bak-Inside-V8-A-Javascript-Virtual-Machine/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文