简体   繁体   English

编译型语言和解释型语言有什么区别?

[英]What's the difference between compiled and interpreted language?

After reading some material on this subject I'm still not sure what the difference between a compiled language and an interpreted language is.在阅读了关于这个主题的一些材料后,我仍然不确定编译语言和解释语言之间的区别是什么。 I was told this is one of the differences between Java and JavaScript.有人告诉我这是 Java 和 JavaScript 之间的区别之一。 Would someone please help me in understanding it?有人可以帮助我理解它吗?

What's the difference between compiled and interpreted language?编译型语言和解释型语言有什么区别?

The difference is not in the language;区别在于语言; it is in the implementation .它正在实施中

Having got that out of my system, here's an answer:把它从我的系统中取出来,这是一个答案:

  • In a compiled implementation, the original program is translated into native machine instructions, which are executed directly by the hardware.在编译实现中,原始程序被翻译成本地机器指令,由硬件直接执行。

  • In an interpreted implementation, the original program is translated into something else.在解释实现中,原始程序被翻译成别的东西。 Another program, called "the interpreter", then examines "something else" and performs whatever actions are called for.另一个称为“解释器”的程序然后检查“其他事物”并执行所需的任何操作。 Depending on the language and its implementation, there are a variety of forms of "something else".根据语言及其实现,有多种形式的“其他”。 From more popular to less popular, "something else" might be从更受欢迎到不那么受欢迎,“别的东西”可能是

    • Binary instructions for a virtual machine, often called bytecode , as is done in Lua, Python, Ruby, Smalltalk, and many other systems (the approach was popularized in the 1970s by the UCSD P-system and UCSD Pascal)虚拟机的二进制指令,通常称为bytecode ,就像在 Lua、Python、Ruby、Smalltalk 和许多其他系统中所做的那样(该方法在 1970 年代由 UCSD P-system 和 UCSD Pascal 普及)

    • A tree-like representation of the original program, such as an abstract-syntax tree, as is done for many prototype or educational interpreters原始程序的树状表示,例如抽象语法树,就像许多原型或教育解释器所做的那样

    • A tokenized representation of the source program, similar to Tcl源程序的标记化表示,类似于 Tcl

    • The characters of the source program, as was done in MINT and TRAC源程序的字符,如在 MINT 和 TRAC 中所做的那样

One thing that complicates the issue is that it is possible to translate (compile) bytecode into native machine instructions .使问题复杂化的一件事是可以将(编译)字节码转换为本地机器指令 Thus, a successful intepreted implementation might eventually acquire a compiler.因此,一个成功的解释实现可能最终会获得一个编译器。 If the compiler runs dynamically, behind the scenes, it is often called a just-in-time compiler or JIT compiler.如果编译器在幕后动态运行,则它通常被称为即时编译器或 JIT 编译器。 JITs have been developed for Java, JavaScript, Lua, and I daresay many other languages. JIT 已经为 Java、JavaScript、Lua 以及我敢说许多其他语言开发。 At that point you can have a hybrid implementation in which some code is interpreted and some code is compiled.那时你可以有一个混合实现,其中一些代码被解释,一些代码被编译。

Java and JavaScript are a fairly bad example to demonstrate this difference , because both are interpreted languages . Java 和 JavaScript 是证明这种差异的一个相当糟糕的例子 ,因为它们都是解释型语言 Java (interpreted) and C (or C++) (compiled) might have been a better example. Java (解释型) 和 C(或 C++) (编译型) 可能是一个更好的例子。

Why the striked-through text?为什么是删除线文本? As this answer correctly points out, interpreted/compiled is about a concrete implementation of a language, not about the language per se .正如这个答案正确指出的那样,解释/编译是关于语言的具体实现,而不是关于语言本身 While statements like "C is a compiled language" are generally true, there's nothing to stop someone from writing a C language interpreter.虽然像“C 是一种编译语言”这样的陈述通常是正确的,但没有什么可以阻止某人编写 C 语言解释器。 In fact, interpreters for C do exist .事实上, C 的解释器确实存在

Basically, compiled code can be executed directly by the computer's CPU.基本上,编译后的代码可以直接由计算机的 CPU 执行。 That is, the executable code is specified in the CPU's "native" language ( assembly language ).也就是说,可执行代码是用 CPU 的“原生”语言(汇编语言)指定的。

The code of interpreted languages however must be translated at run-time from any format to CPU machine instructions.然而,解释语言的代码必须在运行时从任何格式转换为 CPU 机器指令。 This translation is done by an interpreter.该翻译由口译员完成。

Another way of putting it is that interpreted languages are code is translated to machine instructions step-by-step while the program is being executed, while compiled languages have code has been translated before program execution.把它的另一种方式是,解释型 语言的 代码转换为机器指令一步一步正在执行程序,当编译 语言有 代码程序执行之前被翻译。

Here is the Basic Difference between Compiler vs Interpreter Language.这是编译器与解释器语言之间的基本区别。

Compiler Language编译器语言

  • Takes entire program as single input and converts it into object code which is stored in the file.将整个程序作为单个输入并将其转换为存储在文件中的目标代码。
  • Intermediate Object code is generated生成中间对象代码
  • eg: C,C++例如:C,C++
  • Compiled programs run faster because compilation is done before execution.编译后的程序运行速度更快,因为编译是在执行之前完成的。
  • Memory requirement is more due to the creation of object code.由于目标代码的创建,内存需求更多。
  • Error are displayed after the entire program is compiled整个程序编译后报错
  • Source code ---Compiler ---Machine Code ---Output源代码---编译器---机器代码---输出

Interpreter Language:口译语言:

  • Takes single instruction as single input and executes instructions.将单个指令作为单个输入并执行指令。
  • Intermediate Object code is NOT generated不生成中间目标代码
  • eg: Perl, Python, Matlab例如:Perl、Python、Matlab
  • Interpreted programs run slower because compilation and execution take place simultaneously.解释程序运行速度较慢,因为编译和执行同时进行。
  • Memory requirement is less.内存需求较少。
  • Error are displayed for every single instruction.每条指令都会显示错误。
  • Source Code ---Interpreter ---Output源代码---解释器---输出

A compiler, in general, reads higher level language computer code and converts it to either p-code or native machine code.通常,编译器读取高级语言计算机代码并将其转换为 p 代码或本机机器代码。 An interpreter runs directly from p-code or an interpreted code such as Basic or Lisp.解释器直接从 p 代码或解释过的代码(如 Basic 或 Lisp)运行。 Typically, compiled code runs much faster, is more compact, and has already found all of the syntax errors and many of the illegal reference errors.通常,编译后的代码运行得更快,更紧凑,并且已经发现了所有的语法错误和许多非法引用错误。 Interpreted code only finds such errors after the application attempts to interpret the affected code.只有在应用程序尝试解释受影响的代码后,解释代码才会发现此类错误。 Interpreted code is often good for simple applications that will only be used once or at most a couple times, or maybe even for prototyping.解释代码通常适用于只使用一次或最多使用几次的简单应用程序,甚至可能用于原型设计。 Compiled code is better for serious applications.编译后的代码更适合严肃的应用程序。 A compiler first takes in the entire program, checks for errors, compiles it and then executes it.编译器首先接收整个程序,检查错误,编译它然后执行它。 Whereas, an interpreter does this line by line, so it takes one line, checks it for errors, and then executes it.而解释器会一行一行地执行此操作,因此它需要一行,检查它是否有错误,然后执行它。

If you need more information, just Google for "difference between compiler and interpreter".如果您需要更多信息,只需谷歌搜索“编译器和解释器之间的差异”。

解释型语言在运行时按照shell脚本中的指令执行,编译型语言是一种编译(变成CPU可以理解的汇编语言)然后像c++一样执行的语言。

It is a very murky distinction, and in fact generally not a property of a language itself, but rather of the program you are using to execute code in that language.这是一个非常模糊的区别,实际上通常不是语言本身的属性,而是您用来执行该语言代码的程序的属性。

However, most languages are used primarily in one form or the other, and yes, Java is essentially always compiled, while javascript is essentially always interpreted.然而,大多数语言主要以一种或另一种形式使用,是的,Java 本质上总是被编译,而 javascript 本质上总是被解释。

To compile source code is to run a program on it that generates a binary, executable file that, when run, has the behavior defined by the source.编译源代码就是在其上运行一个程序,该程序生成一个二进制可执行文件,该文件在运行时具有源代码定义的行为。 For instance, javac compiles human-readbale .java files into machine-readable .class files.例如,javac 将人类可读的 .java 文件编译成机器可读的 .class 文件。

To interpret source code is run a program on it that produces the defined behavior right away, without generating an intermediary file.解释源代码是在其上运行一个程序,该程序立即产生定义的行为,而不生成中间文件。 For instance, when your web browser loads stackoverflow.com, it interprets a bunch of javascript (which you can look at by viewing the page source) and produces lots of the nice effects these pages have - for instance, upvoting, or the little notifier bars across the top.例如,当您的 Web 浏览器加载 stackoverflow.com 时,它会解释一堆 javascript(您可以通过查看页面源代码来查看)并产生这些页面具有的许多不错的效果 - 例如,upvoting 或小通知程序横在顶部的酒吧。

As other have said, compiled and interpreted are specific to an implementation of a programming language;正如其他人所说,编译解释特定于编程语言的实现 they are not inherent in the language.它们不是语言固有的。 For example, there are C interpreters.例如,有 C 解释器。

However, we can (and in practice we do) classify programming languages based on its most common (sometimes canonical) implementation.但是,我们可以(并且在实践中确实可以)根据其最常见(有时是规范的)实现对编程语言进行分类。 For example, we say C is compiled.例如,我们说 C 是编译的。

First, we must define without ambiguity interpreters and compilers:首先,我们必须明确定义解释器和编译器:

An interpreter for language X is a program (or a machine, or just some kind of mechanism in general) that executes any program p written in language X such that it performs the effects and evaluates the results as prescribed by the specification of X .语言X解释器是一个程序(或机器,或者只是一般的某种机制),它执行用语言X编写的任何程序p ,以便它执行X规范规定的效果并评估结果。

A compiler from X to Y is a program (or a machine, or just some kind of mechanism in general) that translates any program p from some language X into a semantically equivalent program p' in some language Y in such a way that interpreting p' with an interpreter for Y will yield the same results and have the same effects as interpreting p with an interpreter for X .XY 的编译器是一个程序(或机器,或只是某种一般的机制),它将来自某种语言X 的任何程序p翻译成某种语言Y中语义上等效的程序p' ,这样解释p '使用Y的解释器将产生相同的结果并具有与使用X的解释器解释p相同的效果。

Notice that from a programmer point of view, CPUs are machine interpreters for their respective native machine language.请注意,从程序员的角度来看,CPU 是其各自本地机器语言的机器解释器。

Now, we can do a tentative classification of programming languages into 3 categories depending on its most common implementation:现在,我们可以根据最常见的实现将编程语言分为 3 类:

  • Hard Compiled languages: When the programs are compiled entirely to machine language.硬编译语言:当程序完全编译为机器语言时。 The only interpreter used is a CPU.唯一使用的解释器是 CPU。 Example: Usually, to run a program in C, the source code is compiled to machine language, which is then executed by a CPU.示例:通常,要在 C 中运行程序,将源代码编译为机器语言,然后由 CPU 执行。
  • Interpreted languages: When there is no compilation of any part of the original program to machine language.解释型语言:当原始程序的任何部分都没有编译成机器语言时。 In other words, no new machine code is generated;换句话说,不会产生新的机器码; only existing machine code is executed.只执行现有的机器代码。 An interpreter other than the CPU must also be used (usually a program).Example: In the canonical implementation of Python, the source code is compiled first to Python bytecode and then that bytecode is executed by CPython, an interpreter program for Python bytecode .还必须使用 CPU 以外的解释器(通常是程序)。示例:在 Python 的规范实现中,源代码首先被编译为Python 字节码,然后该字节码由 CPython 执行,CPython 是Python 字节码的解释器程序。
  • Soft Compiled languages: When an interpreter other than the CPU is used but also parts of the original program may be compiled to machine language.软编译语言:当使用 CPU 以外的解释器但也可以将原始程序的一部分编译为机器语言时。 This is the case of Java, where the source code is compiled to bytecode first and then, the bytecode may be interpreted by the Java Interpreter and/or further compiled by the JIT compiler.这是 Java 的情况,其中源代码首先被编译为字节码,然后字节码可以由 Java 解释器解释和/或由 JIT 编译器进一步编译。

Sometimes, soft and hard compiled languages are refered to simply compiled, thus C#, Java, C, C++ are said to be compiled.有时,软编译语言和硬编译语言被称为简单编译,因此 C#、Java、C、C++ 被称为编译。

Within this categorization, JavaScript used to be an interpreted language, but that was many years ago.在这个分类中,JavaScript 曾经是一种解释型语言,但那是多年前的事了。 Nowadays, it is JIT-compiled to native machine language in most major JavaScript implementations so I would say that it falls into soft compiled languages.如今,在大多数主要的 JavaScript 实现中,它被 JIT 编译为本地机器语言,所以我会说它属于软编译语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM