Compiler Vs. interpreter: explanation and differences
A computer program is usually written in high level language described as a source code. The difference between an interpreter and compiler is the point at which a source code is actually executed. This means that when convert source code into machine code , we use either a compiler or an interpreter. So generally categorizing computer languages by "compiled" or "interpreted" doesn't make much sense. Now a days, interpreting Vs. compiling is a trade-off, with time spent compiling often being rewarded by better runtime performance , but an interpretative environment giving more opportunities for interaction .
What is a Compiler?
A compiler is a computer program that transforms source code written in a programming language into another computer language. It generates binary executable in the target machine's native executable format, which can then be assembled and linked to the appropriate machine op-codes to allow the program to execute. This binary executable contains all required resources except for system libraries . It's ready to run with no further preparation and processing and it runs like lightning because the code is the native code for the CPU on the target machine . A compiled program is not human readable, but instead is in an architecture-specific machine language. Compilation usually takes pretty long and tries to do lots of expensive optimization so that the resulting executable runs faster.example :
Some compilers compile not to CPU-specific machine instructions but to bytecode, a kind of artificial machine code for a fictitious machine. This makes the compiled program a bit more portable, but requires a bytecode interpreter on every target system.
What is an Interpreter?
An interpreter is also a computer program that translates a high-level language into a low-level one, but it does it at the moment the program is running. This means that an interpreter accepts source code expressed in a programming language , and immediately executes that code. It works by fetching, analysing, and executing one instruction at a time . This can be contrasted with a compiled language which is converted into machine code and then 'directly' executed by the host CPU . The Interpreters translates one statement into machine language, executes it, and proceeds to next statement. It generates machine-independent code which can then be on-the-fly compiled to assembly code (e.g. Just-in-Time compilation). In general, interpreted programs are slower than compiled programs, but are easier to debug and revise .example :
Many interpreters will pre-compile the code they're given so the translation step doesn't have to be repeated again and again.
- Spends a lot of time analyzing and processing the source code.
- The resulting executable is some form of machine-specific binary code.
- The computer hardware executes (interprets) the resulting code.
- Program execution is fast.
- Relatively little time is spent analyzing and processing the source code.
- The resulting code is some sort of intermediate code (bytecode).
- The resulting code is interpreted by another program.
- Program execution is relatively slow.
Intermediate to computer-specific compiled programs and interpreted scripts are programs designed for runtime environments. Java and Smalltalk programs are executed in this fashion. Some other computer languages, which are compiled as well as interpreted , are Scala, Haskell or Ocaml. Each of these languages has an interactive interpreter, as well as a compiler to byte-code or native machine code.
NEXT.....Difference between Data and Information