我想更好地理解其中的区别。我在网上找到了很多解释,但它们都倾向于抽象的差异,而不是实际的含义。
Most of my programming experiences has been with CPython (dynamic, interpreted), and Java (static, compiled). However, I understand that there are other kinds of interpreted and compiled languages. Aside from the fact that executable files can be distributed from programs written in compiled languages, are there any advantages/disadvantages to each type? Oftentimes, I hear people arguing that interpreted languages can be used interactively, but I believe that compiled languages can have interactive implementations as well, correct?
开始用“过去的冲击波”来思考
很久很久以前,有一个计算机王国
解释器和编译器。的优点引起了各种各样的争论
一个比另一个。当时的普遍意见是这样的:
解释器:快速开发(编辑和运行)。执行速度慢,因为每个语句都必须被解释为
每次执行的机器代码(想想这对于执行了数千次的循环意味着什么)。
编译器:开发(编辑、编译、链接和运行)缓慢。编译/链接步骤可能会花费大量时间)。快
来执行。整个程序已经是原生机器代码了。
运行时有一到两个数量级的差异
解释程序和编译程序之间存在性能差异。其他的区别
点,例如代码的运行时可变性,也有一些兴趣,但主要是
区别围绕着运行时性能问题。
今天的情况已经发展到这样的程度,编译/解释的区别是
几乎无关紧要。许多
编译语言调用的运行时服务并非如此
完全基于机器代码。而且,大多数解释型语言都被“编译”成字节码
之前执行。字节码解释器非常高效,可以与一些编译器生成的解释器相匹敌
从执行速度的角度来看代码。
典型的区别是编译器生成本机机器码,解释器读取源代码
使用某种运行时系统动态生成机器代码。
如今,经典的诠释者已所剩无几——几乎全部
编译成字节码(或其他一些半编译状态),然后在虚拟“机器”上运行。
我猜这是计算机科学中最大的误解之一。
因为解释和编译是完全不同的两件事,我们不能用这种方式进行比较。
编译是将一种语言翻译成另一种语言的过程。编译的类型很少。
编译-将高级语言转换为机器/字节代码(例如:C/ c++ /Java)
翻译——将高级语言翻译成另一种高级语言(例如:TypeScript)
解释是实际执行程序的过程。这可能以几种不同的方式发生。
Machine level interpretation - This interpretation happens to the code which is compiled into machine code. Instructions are directly interpreted by the processor. Programming languages like C/C++ generate machine code, which is executable by the processor. So the processor can directly execute these instructions.
Virtual machine level interpretation - This interpretation happens to the code which is not compiled into the machine level (processor support) code, but into some intermediate-level code. This execution is done by another software, which is executed by the processor. At this time actually processor doesn't see our application. It just executing the virtual machine, which is executing our application. Programming languages like Java, Python, C# generate a byte code, which is executable by the virtual interpreter/machine.
所以在一天结束的时候,我们必须明白的是,世界上所有的编程语言都应该在某个时候进行解释。它可以由处理器(硬件)或虚拟机完成。
编译只是将我们编写的人类可理解的高级代码带到机器可理解的硬件/软件级别的过程。
这是完全不同的两件事,我们无法比较。但是这些术语非常适合教给初学者编程语言是如何工作的。
PS:
Some programming languages like Java have a hybrid approach to do this. First, compile the high-level code into byte code which is virtual-machine readable. And on the fly, a component called the JIT compiler compiles byte-code into machine code. Specifically, code lines that are executed again and again many times are get translated into the machine language, which makes the interpretation process much faster. Because hardware processor is always much faster than virtual interpreter/processor.
Java JIT编译器如何工作