我想更好地理解其中的区别。我在网上找到了很多解释,但它们都倾向于抽象的差异,而不是实际的含义。

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?


当前回答

Compile is the process of creating an executable program from code written in a compiled programming language. Compiling allows the computer to run and understand the program without the need of the programming software used to create it. When a program is compiled it is often compiled for a specific platform (e.g. IBM platform) that works with IBM compatible computers, but not other platforms (e.g. Apple platform). The first compiler was developed by Grace Hopper while working on the Harvard Mark I computer. Today, most high-level languages will include their own compiler or have toolkits available that can be used to compile the program. A good example of a compiler used with Java is Eclipse and an example of a compiler used with C and C++ is the gcc command. Depending on how big the program is it should take a few seconds or minutes to compile and if no errors are encountered while being compiled an executable file is created.check this information

其他回答

解释源代码相对于编译源代码的最大优势是可移植性。

如果你的源代码是编译的,你需要为你的程序运行在不同类型的处理器和/或平台编译不同的可执行文件(例如一个用于Windows x86,一个用于Windows x64,一个用于Linux x64,等等)。此外,除非您的代码完全符合标准,并且不使用任何特定于平台的函数/库,否则您实际上需要编写和维护多个代码库!

如果你的源代码是解释型的,你只需要编写一次,它就可以在任何平台上由合适的解释器解释和执行!它是便携!请注意,解释器本身是为特定平台编写和编译的可执行程序。

编译代码的一个优点是它向最终用户隐藏了源代码(可能是知识产权),因为您部署的不是人类可读的原始源代码,而是一个模糊的二进制可执行文件。

开始用“过去的冲击波”来思考

很久很久以前,有一个计算机王国 解释器和编译器。的优点引起了各种各样的争论 一个比另一个。当时的普遍意见是这样的:

解释器:快速开发(编辑和运行)。执行速度慢,因为每个语句都必须被解释为 每次执行的机器代码(想想这对于执行了数千次的循环意味着什么)。 编译器:开发(编辑、编译、链接和运行)缓慢。编译/链接步骤可能会花费大量时间)。快 来执行。整个程序已经是原生机器代码了。

运行时有一到两个数量级的差异 解释程序和编译程序之间存在性能差异。其他的区别 点,例如代码的运行时可变性,也有一些兴趣,但主要是 区别围绕着运行时性能问题。

今天的情况已经发展到这样的程度,编译/解释的区别是 几乎无关紧要。许多 编译语言调用的运行时服务并非如此 完全基于机器代码。而且,大多数解释型语言都被“编译”成字节码 之前执行。字节码解释器非常高效,可以与一些编译器生成的解释器相匹敌 从执行速度的角度来看代码。

典型的区别是编译器生成本机机器码,解释器读取源代码 使用某种运行时系统动态生成机器代码。 如今,经典的诠释者已所剩无几——几乎全部 编译成字节码(或其他一些半编译状态),然后在虚拟“机器”上运行。

简短的(不精确的)定义:

编译语言:将整个程序立即转换为机器代码,然后由CPU运行机器代码。

解释语言:逐行读取程序,一旦读取一行,CPU就会执行该行的机器指令。

但实际上,现在很少有语言是纯编译或纯解释的,它们通常是混合的。想要更详细的图片描述,请看这个帖子:

编译和解释的区别是什么?

或者是我后来的博客:

https://orangejuiceliberationfront.com/the-difference-between-compiler-and-interpreter/

语言本身既不编译也不解释,只有语言的特定实现才是。Java就是一个很好的例子。有一个基于字节码的平台(JVM)、一个本机编译器(gcj)和一个用于Java超集(bsh)的互用器。那么Java现在是什么呢?字节码编译,本机编译还是解释?

其他既编译又解释的语言有Scala、Haskell或Ocaml。每种语言都有一个交互式解释器,以及一个字节码或本机机器码的编译器。

所以一般来说,用“编译型”和“解释型”来划分语言并没有多大意义。

我猜这是计算机科学中最大的误解之一。 因为解释和编译是完全不同的两件事,我们不能用这种方式进行比较。

编译是将一种语言翻译成另一种语言的过程。编译的类型很少。

编译-将高级语言转换为机器/字节代码(例如: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编译器如何工作