1.4.编译型语言 vs 解释型语言
编译型语言和解释型语言的优缺点对比
编程语言可以分为:完全的编译型,纯粹的解释型或者两者相互混合的编程语言。实际上,大部分现有的编程语言都同时拥有其编译型和解释型版本。 编译型语言和解释型语言分别有其各自的优势和不足。下面从编译型语言开始介绍。
编译型语言:
- 编译型语言最大的优势之一就是其执行速度。用C/C++编写的程序运行速度要比用Java编写的相同程序快30%-70%。
- 编译型程序比解释型程序消耗的内存更少。
- 不利的一面——编译器比解释器要难写得多。
- 编译器在调试程序时提供不了多少帮助——有多少次在你的C语言代码中遇到一个“空指针异常”时,需要花费好几个小时来明确错误到底在代码中的什么位置。
- 可执行的编译型代码要比相同的解释型代码大许多。例如,C/C++的.exe文件要比同样功能的Java的.class文件大很多。
- 编译型程序是面向特定平台的因而是平台依赖的。
- 编译型程序不支持代码中实现安全性——例如,一个编译型的程序可以访问内存的任何区域,并且可以对你的PC做它想做的任何事情(大部分病毒是使用编译型语言编写的)
- 由于松散的安全性和平台依赖性,编译型语言不太适合开发因特网或者基于Web的应用。
解释型语言:
- 解释型语言提供了极佳的调试支持。一名Java程序员只需要几分钟就可以定位并修复一个“空指针异常”,因为Java运行环境不仅指明了异常的性质,而且给出了异常发生位置具体的行号和函数调用顺序(著名的堆栈跟踪信息)。这样的便利是编译型语言所无法提供的。
- 另一个优势是解释器比编译器容易实现
- 解释型语言最大的优势之一是其平台独立性
- 解释型语言也可以保证高度的安全性——这是互联网应用迫切需要的
- 中间语言代码的大小比编译型可执行代码小很多
- 平台独立性,以及严密的安全性是使解释型语言成为适合互联网和Web应用的理想语言的2个最重要的因素。
- 解释型语言存在一些严重的缺点。解释型应用占用更多的内存和CPU资源。这是由于,为了运行解释型语言编写的程序,相关的解释器必须首先运行。解释器是复杂的,智能的,大量消耗资源的程序并且它们会占用很多CPU周期和内存。
- 由于解释型应用的decode-fetch-execute(解码-抓取-执行)的周期,它们比编译型程序慢很多。
- 解释器也会做很多代码优化,运行时安全性检查;这些额外的步骤占用了更多的资源并进一步降低了应用的运行速度。
原文链接: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
- 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.
- Compiled code also takes less memory as compared to an interpreted program.
- On the down side - a compiler is much more difficult to write than an interpreter.
- 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.
- 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
- Compiled programs are targeted towards a particular platform and hence are platform dependent.
- 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).
- 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
- 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.
- Another advantage is that Interpreters are much easier to build then a compiler.
- One of the biggest advantages of Interpreters is that they make platform-independence possible.
- Interpreted language also allow high degree of security - something badly needed for an Internet application.
- An intermediate language code size is much smaller than a compiled executable code.
- Platform independence, and tight security are the two most important factors that make an interpreted language ideally suited for Internet and web-based applications.
- 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.
- Due to interpreted application's decode-fetch-execute cycle; they are much slower than compiled programs.
- 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.