Every program is a set of instructions, whether it’s to add two numbers or send a request over the internet. Compilers and interpreters take human-readable code and convert it to computer-readable machine code.

https://www.freecodecamp.org/news/compiled-versus-interpreted-languages/

Compiler

Compilers will translate developers’s whole code into the machine code so that the computer will receive the entire machine code at one go, and run it.

During the translation, the compilers will notify if there are any bugs, and the developers must fix all the errors.

Interpreters

Interpreters run through a program line by line, and translate each line into machine code, and the computer will run it every moment it receives the code from the interpreter.

For example, the first line says x = 5, the interpreter translates it into machine code and the machine will run it. Similarly, the second line will also translated into machine code and the computer will run every moment it receives the code from the interpreter. The machine doesn’t need to wait for the whole script to be translated.

However, Interpreted languages were once significantly slower than compiled languages. Because every time the computer finishes executing the line (which has been translated into machine code already), it has to stop and wait for the interpreter to finish translating the next line. meanwhile, for the compiling language, the computer doesn’t need to wait for anything while executing the whole program.

But, with the development of just-in-time compilation, that gap is shrinking.

JIT compiling

Byte code

Some languages like Python, Java, and C# both have interpreting and compiling. During the translation into machine code, they produce a type of intểmdiate code, or low-level code called bytecode. Take a quick look at bytecode here: https://hieuphan.com/what-is-python-byte-code/

  • Source code is written by developers in high programming language
  • The compiler generates the bytecode
  • Bytecode is taken by a Virtual Machine and translated into machine code
  • Computer execute program by reading machine code

JIT

In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations)[1] is compilation (of computer code) during execution of a program (at run time) rather than before execution.[2] This may consist of source code translation but is more commonly bytecode translation to machine code, which is then executed directly.

A system implementing a JIT compiler typically continuously analyses the code being executed and identifies parts of the code where the speedup gained from compilation or recompilation would outweigh the overhead of compiling that code.

Here I will give an example of a simple technique to reduce the number of translations from bytecode to machine code, each time the interpreter translates each line of code to machine code, it will cache the result somewhere. The next time, instead of translating, it will retrieve the available machine code and send it to the computer to execute, thereby saving a lot of time.

References

Categorized in:

Tagged in: