# 解释器（计算机）

解释器是一种在软件层面执行用另一种语言（即客语言）编写的代码的程序，通常通过求值源代码或执行中间表示来实现；与纯粹的提前编译方式不同，它无需在执行前生成原生可执行文件。<sup>[1](https://www.jot.fm/issues/issue_2026_01/a15.pdf)</sup> 包括 Bash、Java、[JavaScript](https://www.edgepedia.org/zh/javascript)、Lua、PHP、Python、R、Ruby 和 SQL 在内的成熟语言都依赖解释器。<sup>[1](https://www.jot.fm/issues/issue_2026_01/a15.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 基本机制 | 解释器在软件层面执行客语言代码，通常通过求值源代码或执行中间表示来实现；提前编译器则在程序运行之前生成可执行代码。<sup>[2](https://www.bbc.co.uk/bitesize/guides/zkcck2p/revision/3)</sup> |
| 实测速度差距 | 在 12 项基准测试中，V8/Node.js 和 CPython 平均比 GCC 慢 8.01 倍和 29.50 倍，CPython 在最差情况下（排序）慢 129.66 倍；经 JIT 编译的 OpenJDK 和 Go 仅慢 1.43 倍和 1.30 倍。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup> |
| 单循环对比 | 用 clang -O2 编译的 C 耗时 0.17 秒，启用 JIT 的 V8 耗时 0.25 秒，仅用解释器的 V8 耗时 0.67 秒，CPython 3.14 的字节码解释器耗时 3.3 至 3.7 秒。<sup>[4](https://www.computerinternals.app/en/levels/programming-languages/compiled-interpreted-jit)</sup> |
| CPython 的分派循环 | 一个遍历字节码指令的循环，每条指令通过一个 switch 语句执行，每个操作码对应一个 case 分支。<sup>[5](https://github.com/python/cpython/blob/f23a1837/InternalDocs/interpreter.md)</sup> |
| 特化收益 | CPython 的特化自适应解释器（PEP 659）带来 10% 至 60% 的加速，主要集中在属性查找、全局变量和调用上。<sup>[6](https://peps.python.org/pep-0659/)</sup> |
| CPython JIT 现状 | 在 Python 3.15 中，JIT 在实测的一级平台上相较解释器带来约 4% 至 12% 的几何平均提升；Python 3.17 自由线程构建的目标是至少 20%。<sup>[7](https://peps.python.org/pep-0836/)</sup> |
| 内存开销 | 在一项研究中，CPython 平均比 GCC 多用 2.12 倍内存，是所研究运行时中最低的，最差情况为排序基准测试上的 4.06 倍。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup> |

## 解释器是什么

解释器在软件层面执行客语言代码，通常通过求值源代码或执行中间表示来实现；提前编译器则在执行开始之前生成可执行代码。<sup>[2](https://www.bbc.co.uk/bitesize/guides/zkcck2p/revision/3)</sup> 解释执行可以避免在执行前生成单独的原生可执行文件，不过解释器也可能先解析源代码或将其编译为中间表示。<sup>[8](https://user.it.uu.se/~kostis/Teaching/KT2-04/jit_survey.pdf)</sup> 纯解释执行往往更慢，因为执行过程会带来解释器分派和求值的开销；JIT 编译可以将频繁执行的代码编译为机器码，从而缩小差距。<sup>[2](https://www.bbc.co.uk/bitesize/guides/zkcck2p/revision/3)</sup>

Python 文档说明了这个词在实践中如何延伸：宿主线程所调用的运行时“解释器”，与在运行已编译 Python 代码的线程中通常运行的“字节码解释器”并不是一回事。<sup>[9](https://docs.python.org/3/reference/executionmodel.html)</sup>

## 解释器如何工作

**解析与程序表示。** 树遍历解释器逐个节点地求值抽象语法树（AST），这是最简单的设计，也是运行最慢的。另一种方式是将 AST 编译为[字节码](https://www.edgepedia.org/zh/bytecode)，即抽象机器的指令，由虚拟机执行；CPython 就是这样工作的。<sup>[10](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)</sup>

**分派循环。** CPython 的字节码解释器是一个遍历字节码指令的循环，每条指令通过一个 switch 语句执行，该语句为每个操作码设有一个实现分支；这个 switch 由 Python/bytecodes.c 文件中用领域特定语言编写的指令定义生成。<sup>[5](https://github.com/python/cpython/blob/f23a1837/InternalDocs/interpreter.md)</sup> 调用 PyEval_EvalCode() 运行一个 CodeObject 会创建一个 Frame，随后调用 _PyEval_EvalFrame() 在新帧中运行该代码对象。<sup>[5](https://github.com/python/cpython/blob/f23a1837/InternalDocs/interpreter.md)</sup>

**分派策略。** 分派本身的成本可以降低。子程序线程化将虚拟指令体实现为可调用例程，在减少分支预测失败方面比分支复制、分支内联、应用-返回内联或微小内联更有效。<sup>[11](https://www.cs.toronto.edu/~matz/dissertation/posted/matzDissertation-Jul21.pdf)</sup> 还可以通过在加载时预解码指令或通过二进制翻译来消除取指/解码开销，从而削减开销。<sup>[12](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/12/asplos-1996.pdf)</sup>

## 解释器架构

解释器设计形成一个谱系，从在运行前决定一切到在运行时决定一切。<sup>[10](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)</sup>

- **树遍历解释器**直接逐个节点地求值 AST；它们通常易于构建，但性能取决于具体实现。<sup>[10](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)</sup>
- **字节码解释器**将 AST 编译为抽象机器的指令，如 CPython 所做的那样。<sup>[10](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)</sup>
- **JIT 混合型**在虚拟机内嵌入编译器，在运行时编译频繁执行的字节码，如 Java 的 HotSpot 所做的那样。<sup>[13](https://cs3110.github.io/textbook/_sources/chapters/interp/intro.md)</sup>
- **提前编译器**如 gcc、clang 和 rustc 在程序运行前翻译整个程序。<sup>[10](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)</sup>

许多高级语言虚拟机将解释器与最终接管执行的 JIT 编译器配对，因为解释中间表示不如执行原生代码高效。<sup>[1](https://www.jot.fm/issues/issue_2026_01/a15.pdf)</sup>

**V8 的分层阶梯**详细展示了这种混合模式：Ignition 立即解释字节码，Sparkplug 在执行足够多的次数后对函数进行基线编译，Maglev 利用稳定的类型反馈优化函数，TurboFan 处理最热的函数，如果类型假设失败，执行会去优化回 Ignition。<sup>[14](https://readoss.com/en/v8/v8/v8-multi-tier-compilation-pipeline)</sup> Ignition 可以无需等待昂贵的编译就开始执行代码。<sup>[15](https://chromium.googlesource.com/v8/v8/+show/main/docs/interpreter/interpreter-ignition.md)</sup>

**AST 与字节码。** 这两种表示最好理解为谱系的两端，而非截然不同的设计，因为序列化的 AST 同时是线性的和树状的。<sup>[1](https://www.jot.fm/issues/issue_2026_01/a15.pdf)</sup> 一项对用 RPython 和 GraalVM 构建的四种解释器的对照研究发现，在仅解释执行时，AST 解释器与字节码解释器相当或略快，在 JIT 编译后也大致相当，这反驳了字节码解释器具有性能优势的民间说法。字节码在内存中比 AST 更紧凑，这对较大的应用很重要。<sup>[16](https://dl.acm.org/doi/10.1145/3622808)</sup>

## 性能：用数据说话

**与提前编译的对比。** 一项 USENIX ATC 2022 的测量研究在 12 项基准测试中发现，V8/Node.js 和 CPython 平均比 GCC 慢 8.01 倍和 29.50 倍，CPython 在排序基准测试的极端情况下慢 129.66 倍。经 JIT 编译的运行时缩小了大部分差距：OpenJDK 和 Go 平均比 GCC 慢 1.43 倍和 1.30 倍，并在 12 项基准测试中的 3 项上胜过 GCC。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup> 在一个紧凑的数值循环上，用 clang -O2 提前编译的 C 耗时 0.17 秒，带 V8 JIT 的 Node.js 25 耗时 0.25 秒，仅用解释器的 V8（node --jitless）耗时 0.67 秒，CPython 3.14 的字节码解释器耗时 3.3 至 3.7 秒；JIT 使 JavaScript 在该循环上的耗时降至 C 的约 1.5 倍以内，而同一引擎仅限解释器时慢了近 3 倍，CPython 的耗时约为 C 的 20 倍。<sup>[4](https://www.computerinternals.app/en/levels/programming-languages/compiled-interpreted-jit)</sup> Benchmarks Game 在每个程序测量前清除文件系统缓存和交换空间，使每个程序都从相似的初始环境开始，其报告的 Python 3 各项基准比率如 2.63 倍和 5.65 倍，还有一项基准测试的实际耗时 311.18 秒对应 1221.85 秒的 CPU 时间，表明解释器的 CPU 开销超过了实际耗时。<sup>[17](https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python3-gpp.html)</sup>

**同一运行时内解释器与 JIT 的对比。** Sun 的 HotSpot 服务器动态编译器在现代个人计算机上运行整个 SPECjvm98 套件的速度比最快的解释器快 4 倍以上。<sup>[11](https://www.cs.toronto.edu/~matz/dissertation/posted/matzDissertation-Jul21.pdf)</sup> PyPy 的追踪 JIT 执行某些 Python 程序的速度比 CPython 快达 50 倍，同时忠实于 Python 的语义。<sup>[18](https://www.cristal.univ-lille.fr/dyla14/papers/dyla14-8-Python_Interpreter_Performance_Deconstructed.pdf)</sup>

**解释器为何慢：逐指令分析。** CPython 执行每条 BINARY_SUBSCR 字节码用了 138 条指令和 41.8 个周期，而 V8 每条 LdaKeyedProperty 花费 90 条指令和 26.3 个周期，因为 V8 的解释器是用中间表示手工打造的，而 CPython 是用 C 实现的。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup> 静态类型也有帮助：OpenJDK 的解释器在数独上分别比 V8 和 CPython 的解释器快 2.59 倍和 5.34 倍，这归因于静态类型消除了运行时类型检查。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup>

**内存与启动。** 在所研究的运行时中，CPython 的峰值内存最接近 GCC，平均多 2.12 倍，最差情况为排序上的 4.06 倍。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup> 启动时间则相反：OpenJDK 和 V8 在数独基准测试上的启动时间分别为 843 毫秒和 788 毫秒，其中 OpenJDK 有 708 毫秒用于从磁盘加载 JVM 二进制文件。<sup>[3](https://www.usenix.org/system/files/atc22-lion.pdf)</sup>

## 与编译器和虚拟机的比较

Java 是编译还是解释并没有明确的答案：javac 将源代码编译为字节码，虚拟机解释该字节码，JIT 在执行期间将频繁执行的部分翻译为机器码。<sup>[10](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)</sup> 同样的混合模式也出现在其他地方：Java 提前编译为 .class 字节码，然后 JVM 解释并 JIT 编译它；GraalVM 的 native-image 则可以完全提前编译它；Android 在安装时部分提前编译应用字节码，其余部分进行 JIT 编译；[WebAssembly](https://www.edgepedia.org/zh/webassembly) 是一种可移植的低级字节码，浏览器在加载时将其编译为机器码。<sup>[4](https://www.computerinternals.app/en/levels/programming-languages/compiled-interpreted-jit)</sup> 混合策略——将源代码编译为字节码，由 JVM 或 OCaml（原 Zinc）虚拟机等虚拟机解释——本身就是一种标准设计。<sup>[13](https://cs3110.github.io/textbook/_sources/chapters/interp/intro.md)</sup>

可分发性也有所不同：解释器不一定生成独立的原生可执行文件；程序可以以源代码或中间表示的形式分发，供兼容的运行时使用。<sup>[2](https://www.bbc.co.uk/bitesize/guides/zkcck2p/revision/3)</sup>

## 语言为何选择解释执行

这些权衡在三方对比中被量化：提前编译具有最高的峰值速度和最小的内存占用，解释执行具有即时启动和最低的峰值速度，JIT 编译在热点代码上接近提前编译的速度，但为编译器、代码缓存和性能分析数据使用更多内存。<sup>[4](https://www.computerinternals.app/en/levels/programming-languages/compiled-interpreted-jit)</sup> 解释执行的程序往往更具可移植性：假设使用与机器无关的表示，要在不同的机器上运行程序，只需提供解释器即可。<sup>[8](https://user.it.uu.se/~kostis/Teaching/KT2-04/jit_survey.pdf)</sup> V8 的 Ignition 解释器存在的意义正是让 JavaScript 无需等待昂贵的编译就能开始执行。<sup>[15](https://chromium.googlesource.com/v8/v8/+show/main/docs/interpreter/interpreter-ignition.md)</sup>

**实现经济性**也很重要。CPython 基于解释器的方法保持了 C API 不变。<sup>[19](https://pablowilliams.github.io/Bytecode/)</sup>

## 缓解速度损失

**特化自适应解释。** 自 Python 3.11 起，CPython 使用了 PEP 659 的特化自适应解释器，它在字节码指令运行时就地将其重写为类型特化的版本，这一过程称为快速化；解释器对其当前操作的类型或值进行推测性特化，并在它们变化时自适应。<sup>[6](https://peps.python.org/pep-0659/)</sup><sup> • </sup><sup>[7](https://peps.python.org/pep-0836/)</sup> 实验表明加速可达 50%，在不同工作负载下为 10% 至 60%；贡献最大的是属性查找、全局变量和调用，较小部分来自改进的分派如超级指令。<sup>[6](https://peps.python.org/pep-0659/)</sup> 这种方法与 JIT 有本质区别：JIT 将热点代码路径编译为完全绕过求值循环的机器码，而特化解释器只是用更廉价的操作码替换现有求值循环中的操作码，分派器仍然存在。<sup>[19](https://pablowilliams.github.io/Bytecode/)</sup> 这也限制了特化能做到的事，因为其优化潜力仅限于单个字节码指令；JIT 则替换整个字节码指令序列，从而实现跨多条指令的优化。<sup>[7](https://peps.python.org/pep-0836/)</sup><sup> • </sup><sup>[20](https://github.com/python/cpython/blob/main/InternalDocs/jit.md)</sup>

**分层 JIT。** Sparkplug 是 V8 的基线编译器，它生成镜像字节码序列的直线原生代码，没有中间表示，以近乎零的编译成本产生约比解释快 2 倍的代码。<sup>[14](https://readoss.com/en/v8/v8/v8-multi-tier-compilation-pipeline)</sup> Maglev 自 Chrome 117 起发布，编译速度约为 TurboFan 的 10 倍，同时达到 Ignition 的 5 至 10 倍吞吐量，面向温热但非极热的函数。<sup>[14](https://readoss.com/en/v8/v8/v8-multi-tier-compilation-pipeline)</sup>

**元编译。** RPython 和 GraalVM 等系统以语言无关的方式提供垃圾回收器和 JIT 编译器，大大减少了实现工作量；两者都达到接近 Node.js/V8 的性能，而作者只需实现一个解释器。<sup>[16](https://dl.acm.org/doi/10.1145/3622808)</sup>

**性能分析引导的目标。** 一项基于采样的性能分析研究在 48 项 pyperformance 基准测试上分析了 CPython，发现大多数解释器组件只贡献了很小一部分开销，最大的成本集中在名称访问操作码和引用计数函数上；它指出基于寄存器的虚拟机架构和基于追踪的垃圾回收是有前景的优化方向，而运算符重载和分派则是无成果的方向。<sup>[21](https://dl.acm.org/doi/abs/10.1016/j.scico.2021.102759)</sup>

## 近期发展

**CPython 的 JIT。** CPython 的 JIT 采用复制-修补设计，这使其有别于 PyPy 的追踪 JIT 和 V8 的方法 JIT；这一想法源自徐浩然（Haoran Xu）和弗雷德里克·约尔斯塔德（Fredrik Kjolstad）2021 年的论文《复制-修补编译》（Copy-and-Patch Compilation），由布兰特·布赫（Brandt Bucher）为 CPython 改编。<sup>[22](https://python-news.com/python-jit-practical-notes-from-production)</sup> 其开发时间线跨越 Python 3.12（字节码重构为微操作）、3.13（追踪投影器、优化器和复制-修补后端，见 PEP 744）、3.14（进一步重构和优化器工作）和 3.15（追踪器重写为记录式，优化器改进）。<sup>[7](https://peps.python.org/pep-0836/)</sup> 工作曾一度暂停：CPython 停止将 JIT 相关工作合并到主分支，直到一项论证 JIT 应作为 CPython 受支持部分的 PEP 被接受，这促成了 PEP 836。<sup>[23](https://lwn.net/Articles/1090385/)</sup> 在 Python 3.15 中，JIT 在实测的一级平台上带来约 4% 至 12% 的几何平均提升，生成的帧可被原生调试器展开回溯，并相对于 3.14 减少了生成代码的内存占用。<sup>[7](https://peps.python.org/pep-0836/)</sup> Python 3.15 alpha 的 JIT 在 macOS AArch64 上测得比尾调用解释器快约 11% 至 12%，在 x86_64 Linux 上比标准解释器快 5% 至 6%，比计划提前一年多（macOS AArch64）和几个月（x86_64 Linux）达到项目设定的启用阈值。<sup>[24](https://blog.python.org/2026/03/jit-on-track/)</sup> PEP 836 设定了初始性能目标：到 Python 3.17 首个贝塔测试版时，JIT 加自由线程构建在 pyperformance 上相较非 JIT 自由线程解释器至少达到 20% 的几何平均提升。<sup>[7](https://peps.python.org/pep-0836/)</sup>

**安全面。** JIT 引入了已知的[攻击面](https://www.edgepedia.org/zh/attack-surface)，因为它在运行时生成可执行数据；CPython 通过确保生成的数据永远不会同时可写和可执行，并将模板存储在静态只读内存中来缓解这一问题。<sup>[7](https://peps.python.org/pep-0836/)</sup>

**V8。** Maglev 自 Chrome 117 起发布，为 Ignition/Sparkplug/TurboFan 阶梯增加了一个中间层。<sup>[14](https://readoss.com/en/v8/v8/v8-multi-tier-compilation-pipeline)</sup>

## 历史

解释器在 20 世纪 70 年代成为系统设计研究的对象。苏斯曼（Sussman）和斯蒂尔（Steele）的麻省理工学院（MIT）人工智能备忘录《解释器的艺术》（The Art of the Interpreter）将自己定位于一个类 LISP 语言家族中，包括 LISP 1.5、MacLISP、InterLISP、CONNIVER、QA4、PLASMA 和 SCHEME。<sup>[25](https://research.scheme.org/lambda-papers/lambda-papers-interpreter.html)</sup> 早期 JIT 编译随之而来：[Sun Microsystems](https://www.edgepedia.org/zh/sun-microsystems) 的早期 Java JIT 编译器，据开发团队 1997 年的报告，将 Java 光线追踪应用提升了 2.2 倍，压缩程序提升了 6.8 倍。<sup>[11](https://www.cs.toronto.edu/~matz/dissertation/posted/matzDissertation-Jul21.pdf)</sup> PEP 659 明确推迟而非拒绝将即时编译作为 CPython 的优化策略。<sup>[6](https://peps.python.org/pep-0659/)</sup>

## References

1. [AST, Bytecode, and the Space In Between: An Exploration of Interpreter Design Tradeoffs, Journal of Object Technology](https://www.jot.fm/issues/issue_2026_01/a15.pdf)
2. [How interpreters and compilers differ, BBC Bitesize (Edexcel GCSE)](https://www.bbc.co.uk/bitesize/guides/zkcck2p/revision/3)
3. [Investigating Managed Language Runtime Performance: Why JavaScript and Python are 8x and 29x slower than C++, yet Java and Go can be Faster? USENIX ATC 2022](https://www.usenix.org/system/files/atc22-lion.pdf)
4. [Compiled, interpreted and JIT-compiled languages, Computer Internals](https://www.computerinternals.app/en/levels/programming-languages/compiled-interpreted-jit)
5. [CPython Internal Docs: interpreter.md](https://github.com/python/cpython/blob/f23a1837/InternalDocs/interpreter.md)
6. [PEP 659, Specializing Adaptive Interpreter](https://peps.python.org/pep-0659/)
7. [PEP 836, JIT Go Brrr: The Path to a Supported JIT Compiler for CPython](https://peps.python.org/pep-0836/)
8. [A Brief History of Just-In-Time (Aycock)](https://user.it.uu.se/~kostis/Teaching/KT2-04/jit_survey.pdf)
9. [Python 3 execution model documentation](https://docs.python.org/3/reference/executionmodel.html)
10. [Why build an interpreter? (Murphy, PL design textbook)](https://www.cs.montana.edu/users/murphy/books/pl-design/foundations/why-interpreter.html)
11. [Design and Implementation of Efficient Virtual Machines (PhD dissertation, University of Toronto)](https://www.cs.toronto.edu/~matz/dissertation/posted/matzDissertation-Jul21.pdf)
12. [The Structure and Performance of Interpreters (Ertl & Gregg, ASPLOS 1996)](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/12/asplos-1996.pdf)
13. [CS 3110: Interpreters and Compilers (Cornell)](https://cs3110.github.io/textbook/_sources/chapters/interp/intro.md)
14. [From Source to Machine Code: V8's Multi-Tier Compilation Pipeline](https://readoss.com/en/v8/v8/v8-multi-tier-compilation-pipeline)
15. [V8 Ignition interpreter design doc](https://chromium.googlesource.com/v8/v8/+show/main/docs/interpreter/interpreter-ignition.md)
16. [AST vs. Bytecode: Interpreters in the Age of Meta-Compilation, PACMPL/OOPSLA](https://dl.acm.org/doi/10.1145/3622808)
17. [Python 3 vs C++ g++, Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python3-gpp.html)
18. [Python Interpreter Performance Deconstructed, DYLA 2014](https://www.cristal.univ-lille.fr/dyla14/papers/dyla14-8-Python_Interpreter_Performance_Deconstructed.pdf)
19. [Bytecode: an interactive monograph on the CPython interpreter](https://pablowilliams.github.io/Bytecode/)
20. [CPython Internal Docs: jit.md](https://github.com/python/cpython/blob/main/InternalDocs/jit.md)
21. [Quantifying the interpretation overhead of Python, Science of Computer Programming](https://dl.acm.org/doi/abs/10.1016/j.scico.2021.102759)
22. [Python JIT: practical notes from production, Python News](https://python-news.com/python-jit-practical-notes-from-production)
23. [A pause for the Python JIT, LWN.net](https://lwn.net/Articles/1090385/)
24. [Python 3.15's JIT is now on track, Python Insider](https://blog.python.org/2026/03/jit-on-track/)
25. [The Art of the Interpreter (Sussman & Steele, the Lambda Papers)](https://research.scheme.org/lambda-papers/lambda-papers-interpreter.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/interpreter-computing

English original: https://www.edgepedia.org/interpreter-computing
