A1.4 · Translation · HL
A1.4 · 翻译过程 · HL
Evaluating Compilers and Interpreters
评价编译器与解释器
Source code cannot run on its own. A compiler and an interpreter both translate it into something the CPU can execute, but they do the job in genuinely different ways, and each way suits a different situation.
源代码本身无法运行。编译器和解释器都会把它翻译成 CPU 能够执行的形式,但两者完成这项工作的方式截然不同,各自适合不同的场景。
Explainer
讲解
Two ways to get a program running
让程序运行起来的两种方式
A CPU only ever executes machine code, so every high-level program has to be translated before it can run. The IB guide asks you to weigh up two different translation processes rather than just name them: compilation and interpretation.
CPU 始终只能执行机器代码,因此每一个高级语言程序在运行前都必须先被翻译。学科指南要求你权衡两种不同的翻译过程,而不只是说出它们的名字:编译与解释。
A compiler translates the entire source-code file in one pass, before the program is ever run. It checks the whole program for syntax errors first, and only once the whole thing is valid does it produce a separate file of object (eventually machine) code that can be executed on its own, any time afterwards, with no compiler present.
编译器在程序运行之前,一次性翻译整个源代码文件。它先检查整个程序有无语法错误,只有当全部内容都通过后,才会生成一个独立的目标代码(最终是机器代码)文件,之后随时都可以单独运行,无需编译器在场。
An interpreter translates and executes the source code one statement at a time, every single time the program is run. No standalone executable is ever produced, so the interpreter itself has to be present on the machine whenever the program runs.
解释器每次运行程序时,都逐条语句地进行翻译并执行。它从不产生独立的可执行文件,因此每次运行程序时,解释器本身都必须存在于这台机器上。
Explore
探索
Watch each translation process step by step
逐步观察两种翻译过程
Toggle between the two processes and step through what actually happens, in order, from source code to a running program.
切换两种过程,按顺序逐步观察从源代码到程序运行的每一步究竟发生了什么。
Explainer
讲解
Weighing the trade-offs
权衡利弊
To evaluate, you weigh strengths against weaknesses against a stated criterion, then reach a judgement. Neither process is simply "better": each one wins on some criteria and loses on others.
要做到评价,就要针对某一标准权衡利弊,然后得出结论。两种过程都不是单纯地"更好":各自在某些标准上占优,在另一些标准上则处于劣势。
Compiled execution
编译执行
Real example: A downloaded desktop game ships as compiled machine code; the player's computer runs it directly, with no source code included.
真实案例:下载的桌面游戏以编译后的机器代码形式发布;玩家的电脑直接运行它,不包含任何源代码。
Advantage
优势
- Runs fast (translation happens once, ahead of time, so the CPU never pays a translation cost while the finished program is actually running)
- 运行速度快(翻译只在事前进行一次,因此成品程序实际运行时,CPU 不需要再承担任何翻译开销)
- Source code can stay private (only the executable is distributed to users, not the original program text)
- 源代码可以保密(分发给用户的只有可执行文件,而非原始程序文本)
Disadvantage
劣势
- Any change needs a full recompile (even a one-line fix means re-running the whole translation process and redistributing a new executable)
- 任何改动都需要完整重新编译(哪怕只改一行代码,也要重新运行整个翻译过程并重新发布可执行文件)
- Errors surface late (a mistake near the end of a long file still isn't reported until the whole file has been scanned)
- 错误暴露得晚(即使错误出现在一个长文件的末尾,也要等到整个文件被扫描完才会被报告)
Interpreted execution
解释执行
Real example: A student debugging a script in an editor re-runs it after every small change and sees the result immediately.
真实案例:学生在编辑器中调试脚本,每做一点小改动就重新运行一次,并立即看到结果。
Advantage
优势
- Faster to test and debug (there's no separate compile step, so edit a line and run again straight away)
- 测试和调试更快(没有单独的编译步骤,改一行代码后可以立即重新运行)
- Stops exactly where the error is (translation and execution happen statement by statement, so the program runs correctly right up to the mistake before it halts)
- 恰好在出错处停止(翻译和执行是逐条语句进行的,所以程序会正确运行到出错处才停下)
Disadvantage
劣势
- Runs slower (a statement inside a loop gets re-translated every single time it executes, not just once)
- 运行速度较慢(循环内的语句每次执行都要重新翻译一次,而不是只翻译一次)
- The interpreter must be present wherever the program runs (there is no standalone executable, so the source code and the interpreter both have to travel together)
- 程序运行的地方都必须有解释器(不存在独立的可执行文件,所以源代码和解释器必须一起传播)
Explore
探索
Which translator fits the job
哪种翻译方式适合这项工作
Click each situation to see which translation process suits it better, and why. This is exactly the reasoning an "evaluate" answer needs to show.
点击每种情境,看看哪种翻译过程更适合,以及原因是什么。这正是"评价"类答案需要展示的推理过程。
Check your understanding
检验你的理解
A programmer is testing a 200-line program and makes a mistake on line 150. Which statement correctly describes how a compiler and an interpreter would each respond?
一名程序员在测试一个 200 行的程序,并在第 150 行犯了一个错误。下列哪项正确描述了编译器和解释器各自的反应?
Common mistake
常见错误
RecurringTwo problems recur. First, answers define each term ("a compiler compiles the code, an interpreter interprets it") without ever weighing them against each other: evaluate needs a stated criterion, a genuine comparison, and a judgement, not a restated definition. Second, some answers claim an interpreted program has no translation step at all: an interpreter does translate, it just does it one statement at a time, at every run, instead of producing a standalone executable ahead of time.
反复出现有两个问题反复出现。第一,答案只是给出定义("编译器编译代码,解释器解释代码"),却从未把两者放在一起权衡:评价需要一个明确的标准、真正的比较,以及一个结论,而不是重述定义。第二,有些答案声称解释执行的程序根本没有翻译步骤:解释器确实进行翻译,只是它每次运行都逐条语句地翻译,而不是事先生成一个独立的可执行文件。
Defines instead of evaluating: “A compiler compiles the program into machine code. An interpreter interprets the program and runs it. Compiled languages use compilers and interpreted languages use interpreters.” Two correct definitions, no criterion, no comparison, no judgement: not an evaluation.
只下定义,不做评价: “编译器把程序编译成机器代码。解释器解释并运行程序。编译型语言使用编译器,解释型语言使用解释器。”两个正确的定义,却没有标准、没有比较、没有结论:算不上评价。
Actually evaluates: “For releasing a finished commercial application, a compiler suits the job better: it translates the whole program once, before release, producing a fast, standalone executable that customers can run without installing any translator. The cost is a slower edit-test cycle for the developer during development, but that cost is paid once by the team, not repeatedly by every user, so a compiler is the more appropriate choice here.”
真正做到了评价: “对于发布一款成品商业应用而言,编译器更适合:它在发布前一次性翻译整个程序,生成一个快速的独立可执行文件,用户无需安装任何翻译工具即可运行。代价是开发过程中编辑-测试的循环会变慢,但这一代价只由开发团队承担一次,而不是由每个用户反复承担,因此在这种情况下编译器是更合适的选择。”
Revision questions
复习题
Specimen-paper style. Write an answer, then reveal the marking points and mark yourself.
采用样卷风格。写下答案,然后展开评分要点给自己打分。
[6]A small studio is about to release version 1.0 of a commercial mobile game worldwide. Evaluate the translation process a compiler would use for this release, compared with using an interpreter.
[6]一间小型工作室即将向全球发布一款商业手机游戏的 1.0 版本。请就此次发布,评价使用编译器与使用解释器这两种翻译过程。
Marking points
评分要点
- A compiler translates the whole program once, before release, so the translation cost is paid a single time by the studio rather than by every player.
- It produces a fast, standalone executable that players can run without needing any translator installed on their phone.
- An interpreter would need to be present on every player's device, and would re-translate the game's code during every play session, slowing it down.
- Judgement: for a worldwide commercial release, a compiler is the more appropriate choice, since fast, dependency-free execution for millions of users matters more than a fast development cycle at this stage.
- 编译器在发布前一次性翻译整个程序,因此翻译成本由工作室承担一次,而不是由每个玩家承担。
- 它生成一个快速的独立可执行文件,玩家无需在手机上安装任何翻译工具即可运行。
- 若使用解释器,则每台玩家设备上都需要有解释器,并且每次游玩都会重新翻译游戏代码,从而拖慢速度。
- 结论:对于全球商业发布而言,编译器是更合适的选择,因为此阶段,为数百万用户提供快速、无需依赖的执行比开发周期的快慢更重要。
[6]A teacher wants her students to see, statement by statement, exactly where their code goes wrong while they are learning to program. Evaluate whether a compiler or an interpreter better serves this teaching goal.
[6]一位教师希望学生在学习编程的过程中,能逐条语句地准确看到代码在何处出错。请评价编译器和解释器,哪一种更能服务于这一教学目标。
Marking points
评分要点
- An interpreter translates and executes one statement at a time, so it runs every statement up to the fault before halting, giving immediate, precise feedback.
- A compiler would report syntax errors only after scanning the whole file, without having run any of it, so feedback would be slower and less tied to a live execution point.
- Interpreted testing also means each small fix can be re-run instantly with no separate build step.
- Judgement: an interpreter better serves this teaching goal, since immediate, statement-level feedback matters more here than raw execution speed.
- 解释器逐条语句翻译并执行,因此在出错前会运行每一条语句才停止,给出即时、精确的反馈。
- 编译器要扫描完整个文件才会报告语法错误,且此时还没有运行过任何代码,因此反馈更慢,也不与实时的执行位置相对应。
- 解释执行的测试方式还意味着每次小改动都能立即重新运行,无需单独的构建步骤。
- 结论:解释器更能服务于这一教学目标,因为此处即时的、逐语句的反馈比原始执行速度更重要。
[6]A colleague claims: “Interpreted programs are always slower than compiled ones, so no professional software is ever interpreted.” Evaluate this claim.
[6]一位同事声称:“解释执行的程序总是比编译执行的程序慢,所以专业软件绝不会采用解释执行。”请评价这一说法。
Marking points
评分要点
- The first half is broadly true: re-translating a statement every time it executes does add overhead a compiled program avoids.
- But the second half overstates it: a great deal of professional software (web back ends, browser scripting, data-analysis tools) is interpreted, because faster development, easier debugging or portability outweigh the speed cost for those uses.
- The speed gap is also narrower in practice than the claim suggests, since many interpreters optimise heavily-used code as they run.
- Judgement: the claim identifies a real trade-off but draws too strong a conclusion from it; professional software regularly chooses an interpreter anyway, when other criteria matter more than raw speed.
- 前半句大体成立:每次执行都重新翻译语句,确实会带来编译程序所没有的开销。
- 但后半句结论过头:大量专业软件(网络后端、浏览器脚本、数据分析工具)都是解释执行的,因为对这些用途而言,更快的开发速度、更容易的调试或可移植性,其重要性超过了速度上的代价。
- 实际上速度差距也往往没有这句话暗示的那么大,因为许多解释器会在运行时对高频代码进行优化。
- 结论:这一说法指出了一个真实的取舍,但由此得出的结论过于绝对;当其他标准比原始速度更重要时,专业软件仍然经常选择解释器。
[6]A hobbyist wants to write a short, one-off script to rename a folder of photo files, then never use it again. Evaluate whether it is worth compiling this script.
[6]一名业余爱好者想写一个简短的一次性脚本来重命名一个文件夹中的照片文件,用完就不再使用。请评价是否值得对这个脚本进行编译。
Marking points
评分要点
- Compiling only pays off if the resulting speed-up is used many times; a script run once gets no benefit from faster repeated execution.
- The extra time to compile and the extra step of producing and discarding an executable outweigh any gain for a program that never runs again.
- An interpreter suits this task: the script can be written and run immediately, with any mistake fixed and re-run at once.
- Judgement: it is not worth compiling this script; the interpreted approach is the appropriate choice for a short, one-off task.
- 编译只有在其带来的速度提升被多次利用时才划算;只运行一次的脚本无法从更快的重复执行中获益。
- 对于一个再也不会运行的程序而言,编译所需的额外时间,以及生成并随后丢弃可执行文件这一额外步骤,其代价超过了任何收益。
- 解释器适合这项任务:脚本可以立即写好并运行,任何错误也能立刻修正并重新运行。
- 结论:不值得编译这个脚本;对于这种简短的一次性任务,解释执行是更合适的做法。
[6]Firmware inside a medical implant must respond to a sensor reading within a guaranteed, very short time limit, every single time. Evaluate which translation process suits this requirement.
[6]某医疗植入设备内部的固件必须每一次都在一个有保证的、极短的时间限制内响应传感器读数。请评价哪种翻译过程更适合这一要求。
Marking points
评分要点
- A compiler translates the whole program once before deployment, producing machine code the CPU runs directly with no runtime translation overhead.
- This gives a fast and predictable maximum response time, which a guaranteed time limit requires.
- An interpreter would re-translate each statement at runtime, adding a variable delay, and would need to be present on hardware that is likely very limited in memory.
- Judgement: a compiler is the appropriate, and here effectively necessary, choice, since the safety-critical timing requirement rules out the unpredictable overhead an interpreter would add.
- 编译器在部署前一次性翻译整个程序,生成的机器代码由 CPU 直接运行,不产生任何运行时翻译开销。
- 这带来了快速且可预测的最大响应时间,而这正是有保证的时间限制所要求的。
- 解释器会在运行时重新翻译每条语句,增加不确定的延迟,而且需要在内存很可能非常有限的硬件上安装解释器。
- 结论:编译器是合适的、在此情境下实际上是必要的选择,因为这项安全攸关的时限要求排除了解释器可能带来的不可预测开销。
Flashcards
记忆卡片
Active recall for this statement’s key terms.
针对本条考纲陈述关键术语的主动回忆练习。
Keep this statement’s practice with you
把这条考纲陈述的练习保存下来
Free account: save your progress and unlock every revision question for A1.4.1, and every other IB CS 2027 statement.
免费账户:解锁 A1.4.1 以及所有其他 IB CS 2027 考纲陈述的每一道复习题,并同步你的学习进度。