编译语言和解释语言之间的区别? [英] Difference between compiled and interpreted languages?

查看:46
本文介绍了编译语言和解释语言之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译和解释语言的相对优缺点是什么?

What are the relative strengths and weaknesses of compiled and interpreted languages?

推荐答案

这两种方法都没有明显的优势-如果一种方法总是更好,那么很有可能我们会在任何地方开始使用它!

Neither approach has a clear advantage over the other - if one approach was always better, chances are that we'd start using it everywhere!

通常来说,编译器具有以下优点:

Generally speaking, compilers offer the following advantages:


  1. 由于他们可以预先看到所有代码,因此在生成代码时可以执行许多分析和优化,最终版本的代码执行速度比单独解释每一行要快。

  1. Because they can see all the code up-front, they can perform a number of analyses and optimizations when generating code that makes the final version of the code executed faster than just interpreting each line individually.

编译器通常可以生成与高级代码等效的低级代码。表中的内存查找方面的想法,例如动态调度或继承。这意味着生成的程序需要记住的原始代码信息更少,从而降低了生成程序的内存使用量。

Compilers can often generate low-level code that performs the equivalent of a high-level ideas like "dynamic dispatch" or "inheritance" in terms of memory lookups inside of tables. This means that the resulting programs need to remember less information about the original code, lowering the memory usage of the generated program.

编译器具有以下缺点:

Generally speaking, compilers have the following drawbacks:


  1. 某些语言功能(例如动态类型)难以有效地进行编译,因为编译器在程序真正运行之前无法预测会发生什么。这意味着编译器可能不会生成非常好的代码。

  2. 由于进行所有分析所需的成本,编译器通常具有较长的启动时间。这意味着,在像Web浏览器这样的需要快速加载代码的环境中,编译器可能会变慢,因为它们会优化不会多次运行的短代码。

一般来说,口译员具有以下优点:

Generally speaking, interpreters have the following advantages:


  1. 因为可以读取编写的代码,而不必执行昂贵的操作来生成或优化代码,它们的启动速度通常比编译器要快。

  1. Because they can read the code as written and don't have to do expensive operations to generate or optimize code, they tend to start up faster than compilers.

因为解释器可以看到程序在运行时的功能,解释器可以使用编译器可能看不到的许多动态优化。

Because interpreters can see what the program does as its running, interpreters can use a number of dynamic optimizations that compilers might not be able to see.

通常来说,口译员有以下缺点:

Generally speaking, interpreters have the following disadvantages:


  1. 口译员通常具有较高的记忆力解释器需要在运行时保留有关程序的更多信息。

  1. Interpreters typically have higher memory usage than compilers because the interpreter needs to keep more information about the program available at runtime.

解释器通常会指定

由于解释器的原因,在解释器的代码中需要一些CPU时间。编译器具有互补的优势和劣势,语言运行时将两者的元素结合在一起变得越来越普遍。 Java的JVM是一个很好的例子-Java代码本身是经过编译的,并且最初会对其进行解释。然后,JVM可以查找已运行多次的代码并将其直接编译为机器代码,这意味着热代码获得了编译的好处,而冷代码则没有。 JVM还可以执行许多动态优化,例如内联缓存,从而以编译器通常不会采用的方式提高性能。

Because interpreters and compilers have complementary strengths and weaknesses, it's becoming increasingly common for language runtimes to combine elements of both. Java's JVM is a good example of this - the Java code itself is compiled, and initially it's interpreted. The JVM can then find code that's run many, many times and compile it directly to machine code, meaning that "hot" code gets the benefits of compilation while "cold" code does not. The JVM can also perform a number of dynamic optimizations like inline caching to speed up performance in ways that compilers typically don't.

许多现代的JavaScript实现都使用类似的技巧。大多数JavaScript代码都很简短,并不能做很多事情,因此通常从解释器开始。但是,如果很明显代码在重复运行,那么许多JS引擎将编译该代码(或至少编译一部分代码),并使用标准技术对其进行优化。最终结果是该代码启动速度快(可用于快速加载网页),但是运行得越快。

Many modern JavaScript implementations use similar tricks. Most JavaScript code is short and doesn't do all that much, so they typically start off using an interpreter. However, if it becomes clear that the code is being run repeatedly, many JS engines will compile the code - or at least, compile bits and pieces of it - and optimize it using standard techniques. The net result is that the code is fast at startup (useful for loading web pages quickly) but gets faster the more that it runs.

最后一个细节是语言不会被编译或解释。通常,C代码是经过编译的,但是有可用的C解释器使调试或可视化正在运行的代码变得更容易(它们经常在入门编程类中使用,或者至少在以前是使用。)在某些JS引擎开始编译它之前,它被认为是一种解释语言。一些Python实现纯粹是解释器,但是您可以获取生成本机代码的Python编译器。现在,某些语言比其他语言更容易编译或解释,但是没有什么可以阻止您为任何特定的编程语言编写编译器或解释器。有一个称为 Futamura投影的理论结果表明,可以解释的任何东西都可以编译,例如。

One last detail is that languages are not compiled or interpreted. Usually, C code is compiled, but there are C interpreters available that make it easier to debug or visualize the code that's being run (they're often used in introductory programming classes - or at least, they used to be.) JavaScript used to be thought of as an interpreted language until some JS engines started compiling it. Some Python implementations are purely interpreters, but you can get Python compilers that generate native code. Now, some languages are easier to compile or interpret than others, but there's nothing stopping you from making a compiler or interpreter for any particular programming language. There's a theoretical result called the Futamura projections that shows that anything that can be interpreted can be compiled, for example.

这篇关于编译语言和解释语言之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆