Monday, 14 May 2012

Learn About Compilers and Interpreters

What's the difference?


Before the Java and C# programming languages appeared, computer programs were onlycompiled or interpreted. Languages like Assembly LanguageCC++, Fortran, Pascal were almost always compiled into machine code. Languages like Basic,VbScript and JavaScript were usually interpreted.
So what is the difference between a compiled program and an Interpreted one?

Compiling

To write a program takes these steps:
  1. Edit the Program
  2. Compile the program into Machine code files.
  3. Link the Machine code files into a runnable program (also known as an exe).
  4. Debug or Run the Program
With some languages like Turbo Pascal and Delphi steps 2 and 3 are combined.
Machine code files are self-contained modules of machine code that require linking together to build the final program. The reason for having separate machine code files is efficiency; compilers only have to recompile source code that have changed. The machine code files from the unchanged modules are reused. This is known as Making the application. If you wish to recompile and rebuild all source code then that is known as a Build.
Linking is a technically complicated process where all the function calls between different modules are hooked together, memory locations are allocated for variables and all the code is laid out in memory, then written to disk as a complete program. This is often a slower step than compiling as all the machine code files must be read into memory and linked together.

Interpreting

The steps to run a program via an interpreter are
  1. Edit the Program
  2. Debug or Run the Program
This is a far faster process and it helps novice programmers edit and test their code quicker than using a compiler. The disadvantage is that interpreted programs run much slower than compiled programs. As much as 5-10 times slower as every line of code has to be re-read, then re-processed.

Enter Java and C#

Both of these languages are semi-compiled. They generate an intermediate code that is optimized for interpretation. This intermediate language is independant of the underlying hardware and this makes it easier to port programs written in either to other processors, so long as an interpreter has been written for that hardware.
Java when compiled produces bytecode that is interpreted at runtime by a Java Virtual Machine(JVM). Many JVMs use a Just-In-Time compiler that converts bytecode to native machine code and then runs that code to increases the interpretation speed. In effect the Java source code is compiled in a two-stage process.
C# is compiled into Common Intermediate Language (CIL, which was previously known as Microsoft Intermediate Language MSIL. This is run by the Common Language Runtime (CLR), part of the .NET framework an environment that provides support services such as garbage collection and Just-In-Time compilation.
Both Java and C# employ speedup techniques so the effective speed is almost as fast as a pure compiled language. If the application spends a lot of time doing input and output like reading disk files or running database queries then the speed difference is barely noticeable.

What Does this Mean to me?

Unless you have a very specific need for speed and must increase the frame rate by a couple of frames per second, you can forget about speed. Any of C, C++ or C# will provide sufficient speed for games, compilers, and operating systems.

0 comments:

Post a Comment