编译型语言和解释型语言的优缺点对比

编程语言可以分为:完全的编译型,纯粹的解释型或者两者相互混合的编程语言。实际上,大部分现有的编程语言都同时拥有其编译型和解释型版本。
编译型语言和解释型语言分别有其各自的优势和不足。下面从编译型语言开始介绍。

编译型语言:

  1. 编译型语言最大的优势之一就是其执行速度。用C/C++编写的程序运行速度要比用Java编写的相同程序快30%-70%。
  2. 编译型程序比解释型程序消耗的内存更少。
  3. 不利的一面——编译器比解释器要难写得多。
  4. 编译器在调试程序时提供不了多少帮助——有多少次在你的C语言代码中遇到一个“空指针异常”时,需要花费好几个小时来明确错误到底在代码中的什么位置。
  5. 可执行的编译型代码要比相同的解释型代码大许多。例如,C/C++的.exe文件要比同样功能的Java的.class文件大很多。
  6. 编译型程序是面向特定平台的因而是平台依赖的。
  7. 编译型程序不支持代码中实现安全性——例如,一个编译型的程序可以访问内存的任何区域,并且可以对你的PC做它想做的任何事情(大部分病毒是使用编译型语言编写的)
  8. 由于松散的安全性和平台依赖性,编译型语言不太适合开发因特网或者基于Web的应用。

解释型语言:

  1. 解释型语言提供了极佳的调试支持。一名Java程序员只需要几分钟就可以定位并修复一个“空指针异常”,因为Java运行环境不仅指明了异常的性质,而且给出了异常发生位置具体的行号和函数调用顺序(著名的堆栈跟踪信息)。这样的便利是编译型语言所无法提供的。
  2. 另一个优势是解释器比编译器容易实现
  3. 解释型语言最大的优势之一是其平台独立性
  4. 解释型语言也可以保证高度的安全性——这是互联网应用迫切需要的
  5. 中间语言代码的大小比编译型可执行代码小很多
  6. 平台独立性,以及严密的安全性是使解释型语言成为适合互联网和Web应用的理想语言的2个最重要的因素。
  7. 解释型语言存在一些严重的缺点。解释型应用占用更多的内存和CPU资源。这是由于,为了运行解释型语言编写的程序,相关的解释器必须首先运行。解释器是复杂的,智能的,大量消耗资源的程序并且它们会占用很多CPU周期和内存。
  8. 由于解释型应用的decode-fetch-execute(解码-抓取-执行)的周期,它们比编译型程序慢很多。
  9. 解释器也会做很多代码优化,运行时安全性检查;这些额外的步骤占用了更多的资源并进一步降低了应用的运行速度。

原文链接:http://www.codeproject.com/Articles/1825/The-Common-Language-Runtime-CLR-and-Java-Runtime-E


Pros and cons of compiled and interpreted languages

Languages can be developed either as fully-compiled, pure-interpreted, or hybrid compiled-interpreted. As a matter of fact, most of the current programming languages have both a compiled and interpreted versions available.

Both compiled and interpreted approaches have their advantages and disadvantages. I will start with the compiled languages.

Compiled languages

  1. One of the biggest advantages of Compiled languages is their execution speed. A program written in C/C++ runs 30-70 % faster then an equivalent program written in Java.
  2. Compiled code also takes less memory as compared to an interpreted program.
  3. On the down side - a compiler is much more difficult to write than an interpreter.
  4. A compiler does not provide much help in debugging a program - how many times have you received a "Null pointer exception" in your C code and have spent hours trying to figure out where in your source code did the exception occurred.
  5. The executable Compiled code is much bigger in size than an equivalent interpreted code e.g. a C/C++ .exe file is much bigger than an equivalent Java .class file
  6. Compiled programs are targeted towards a particular platform and hence are platform dependent.
  7. Compiled programs do not allow security to be implemented with in the code - e.g. a compiled program can access any area of the memory, and can do whatever it wants with your PC (most of the viruses are made in compiled languages).
  8. Due to loose security and platform dependence - a compiled language is not particularly suited to be used to develop Internet or web-based applications.

Interpreted languages

  1. Interpreted language provides excellent debugging support. A Java programmer only spends a few minutes fixing a "Null pointer exception", because Java runtime not only specifies the nature of exception but also gives the exact line number and function call sequence (the famous stack trace information) where the exception occurred. This facility is something that a compiled language can never provide.
  2. Another advantage is that Interpreters are much easier to build then a compiler.
  3. One of the biggest advantages of Interpreters is that they make platform-independence possible.
  4. Interpreted language also allow high degree of security - something badly needed for an Internet application.
  5. An intermediate language code size is much smaller than a compiled executable code.
  6. Platform independence, and tight security are the two most important factors that make an interpreted language ideally suited for Internet and web-based applications.
  7. Interpreted languages have some serious drawbacks. The interpreted applications take up more memory and CPU resources. This is because in order to run a program written in interpreted language; the corresponding interpreter must be run first. Interpreters are sophisticated, intelligent and resource hungry programs and they take up lot of CPU cycles and RAM.
  8. Due to interpreted application's decode-fetch-execute cycle; they are much slower than compiled programs.
  9. Interpreters also do lot of code-optimization, security violation checking at run-time; these extra steps take up even more resources and further slows the application down.

本文链接:http://bookshadow.com/weblog/2014/08/13/pros-and-cons-of-compiled-and-interpreted-languages/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

评论
  1. 11 11 发布于 2014年8月13日 20:25 #

    hao

  2. 杺 发布于 2015年11月13日 19:00 #

    java是解释型语言么??

  3. 在线疯狂 在线疯狂 发布于 2015年11月14日 22:19 #

    其实从严格意义上来说,java只能算一种“混合型语言”,但相对于C/C++这种纯粹的编译型语言来说,也可以看成是解释型的。参考知乎:http://www.zhihu.com/question/19608553

张贴您的评论