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

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?


当前回答

从http://www.quora.com/What-is-the-difference-between-compiled-and-interpreted-programming-languages

There is no difference, because “compiled programming language” and “interpreted programming language” aren’t meaningful concepts. Any programming language, and I really mean any, can be interpreted or compiled. Thus, interpretation and compilation are implementation techniques, not attributes of languages. Interpretation is a technique whereby another program, the interpreter, performs operations on behalf of the program being interpreted in order to run it. If you can imagine reading a program and doing what it says to do step-by-step, say on a piece of scratch paper, that’s just what an interpreter does as well. A common reason to interpret a program is that interpreters are relatively easy to write. Another reason is that an interpreter can monitor what a program tries to do as it runs, to enforce a policy, say, for security. Compilation is a technique whereby a program written in one language (the “source language”) is translated into a program in another language (the “object language”), which hopefully means the same thing as the original program. While doing the translation, it is common for the compiler to also try to transform the program in ways that will make the object program faster (without changing its meaning!). A common reason to compile a program is that there’s some good way to run programs in the object language quickly and without the overhead of interpreting the source language along the way. You may have guessed, based on the above definitions, that these two implementation techniques are not mutually exclusive, and may even be complementary. Traditionally, the object language of a compiler was machine code or something similar, which refers to any number of programming languages understood by particular computer CPUs. The machine code would then run “on the metal” (though one might see, if one looks closely enough, that the “metal” works a lot like an interpreter). Today, however, it’s very common to use a compiler to generate object code that is meant to be interpreted—for example, this is how Java used to (and sometimes still does) work. There are compilers that translate other languages to JavaScript, which is then often run in a web browser, which might interpret the JavaScript, or compile it a virtual machine or native code. We also have interpreters for machine code, which can be used to emulate one kind of hardware on another. Or, one might use a compiler to generate object code that is then the source code for another compiler, which might even compile code in memory just in time for it to run, which in turn . . . you get the idea. There are many ways to combine these concepts.

其他回答

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

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

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

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

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

或者是我后来的博客:

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

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

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

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

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

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

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

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

编译语言是这样一种语言:程序一旦编译,就用目标机器的指令来表达。例如,源代码中的加法“+”操作可以直接转换为机器代码中的“ADD”指令。

解释型语言是指指令不直接由目标机器执行,而是由其他程序(通常用本机语言编写)读取和执行的语言。例如,相同的“+”操作将在运行时被解释器识别,然后调用它自己的“add(a,b)”函数,并使用适当的参数,然后执行机器代码“add”指令。

你可以在编译语言中做你在解释语言中可以做的任何事情,反之亦然——它们都是图灵完备的。然而,这两种方法在实施和使用方面都有优点和缺点。

我将完全概括(纯粹主义者原谅我!),但大致来说,以下是编译语言的优点:

通过直接使用目标计算机的本机代码获得更快的性能 有机会在编译阶段应用相当强大的优化

下面是解释型语言的优点:

更容易实现(编写好的编译器非常困难!!) 不需要运行编译阶段:可以直接“动态”执行代码 是否可以更方便地使用动态语言

注意,字节码编译等现代技术增加了一些额外的复杂性——这里发生的情况是,编译器的目标是一个与底层硬件不同的“虚拟机”。这些虚拟机指令可以在稍后阶段再次编译,以获得本机代码(例如,由Java JVM JIT编译器完成)。

极端和简单的情况:

A compiler will produce a binary executable in the target machine's native executable format. This binary file contains all required resources except for system libraries; it's ready to run with no further preparation and processing and it runs like lightning because the code is the native code for the CPU on the target machine. An interpreter will present the user with a prompt in a loop where he can enter statements or code, and upon hitting RUN or the equivalent the interpreter will examine, scan, parse and interpretatively execute each line until the program runs to a stopping point or an error. Because each line is treated on its own and the interpreter doesn't "learn" anything from having seen the line before, the effort of converting human-readable language to machine instructions is incurred every time for every line, so it's dog slow. On the bright side, the user can inspect and otherwise interact with his program in all kinds of ways: Changing variables, changing code, running in trace or debug modes... whatever.

说完了这些,让我来解释一下,生活不再那么简单了。例如,

Many interpreters will pre-compile the code they're given so the translation step doesn't have to be repeated again and again. Some compilers compile not to CPU-specific machine instructions but to bytecode, a kind of artificial machine code for a ficticious machine. This makes the compiled program a bit more portable, but requires a bytecode interpreter on every target system. The bytecode interpreters (I'm looking at Java here) recently tend to re-compile the bytecode they get for the CPU of the target section just before execution (called JIT). To save time, this is often only done for code that runs often (hotspots). Some systems that look and act like interpreters (Clojure, for instance) compile any code they get, immediately, but allow interactive access to the program's environment. That's basically the convenience of interpreters with the speed of binary compilation. Some compilers don't really compile, they just pre-digest and compress code. I heard a while back that's how Perl works. So sometimes the compiler is just doing a bit of the work and most of it is still interpretation.

最后,现在,解释和编译是一种权衡,花费(一次)编译的时间通常会获得更好的运行时性能,但解释环境提供了更多的交互机会。编译与解释主要是“理解”程序的工作如何在不同的过程之间划分的问题,而如今,由于语言和产品试图提供两者的最佳服务,这条界线有点模糊。