编译与解释语言 [英] Compiled vs. Interpreted Languages

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

问题描述

我想更好地了解差异。我在网上发现了很多解释,但他们倾向于抽象的差异而不是实际的含义。



我的编程经验大多是使用CPython(动态,解释)和Java(静态,编译)。但是,我理解还有其他种类的解释和编译语言。除了可执行文件可以用编译语言编写的程序分发的事实外,每种类型有什么优点/缺点?通常,我听说人们认为解释型语言可以交互使用,但我相信编译型语言也可以有交互式实现,正确吗?

解决方案

编译语言是指程序在编译后在目标机器的指令中表达的语言。例如,源代码中的附加+操作可以直接转换为机器码中的ADD指令。



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



您可以在编译语言中使用解释型语言做任何事情,反之亦然 - 它们都是图灵完成的。



我要完全推广(纯粹主义者原谅我!),但是,大致来说,这里有编译语言的优点:




  • 直接使用目标机器的本地代码可以提高性能

  • 在编译阶段强大的优化



以下是解释语言的优点:




  • 更容易实现(编写良好的编译器非常难!)

  • 无需运行编译阶段:


  • b 请注意,现代技术如字节码编译添加一些额外的复杂性 - 这里发生的是编译器目标底层硬件不一样的虚拟机。然后可以在稍后阶段再次编译这些虚拟机指令以获得本地代码(例如,如Java JVM JIT编译器所做的)。


    I'm trying to get a better understanding of the difference. I've found a lot of explanations online, but they tend towards the abstract differences rather than the practical implications.

    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?

    解决方案

    A compiled language is one where the program, once compiled, is expressed in the instructions of the target machine. For example, an addition "+" operation in your source code could be translated directly to the "ADD" instruction in machine code.

    An interpreted language is one where the instructions are not directly executed by the target machine, but instead read and executed by some other program (which normally is written in the language of the native machine). For example, the same "+" operation would be recognised by the interpreter at run time, which would then call its own "add(a,b)" function with the appropriate arguments, which would then execute the machine code "ADD" instruction.

    You can do anything that you can do in an interpreted language in a compiled language and vice-versa - they are both Turing complete. Both however have advantages and disadvantages for implementation and use.

    I'm going to completely generalise (purists forgive me!) but, roughly, here are the advantages of compiled languages:

    • Faster performance by directly using the native code of the target machine
    • Opportunity to apply quite powerful optimisations during the compile stage

    And here are the advantages of interpreted languages:

    • Easier to implement (writing good compilers is very hard!!)
    • No need to run a compilation stage: can execute code directly "on the fly"
    • Can be more convenient for dynamic languages

    Note that modern techniques such as bytecode compilation add some extra complexity - what happens here is that the compiler targets a "virtual machine" which is not the same as the underlying hardware. These virtual machine instructions can then be compiled again at a later stage to get native code (e.g. as done by the Java JVM JIT compiler).

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

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