The source code we write is human language that we can easily understand ourselves; But for computer hardware (CPUS), the source code is a mystery and cannot be executed. The computer can only recognize certain binary instructions and must convert the source code into binary instructions before the program can actually run. So-called binary instructions, or machine code, are hardware-level “code” that the CPU can read. Humble hardware (like ancient single-chip computers) can use only a few dozen instructions, while powerful hardware (PCS and smartphones) can use hundreds of instructions. However, when exactly do you convert source code to binary instructions? Different programming languages have different rules:

  • Some programming languages require that all source code must be converted to binary instruction at one time in advance, that is, to generate an executable program (.exe under Windows), such as C language, C++, Golang, Pascal (Delphi), assembly, etc. This programming language is called compiled language. The conversion tool used is called a compiler.
  • Some programming languages can convert as they go along, converting whatever source code is needed without generating executable programs, such as Python, JavaScript, PHP, Shell, MATLAB, etc. Such programming languages are called interpreted languages, and the conversion tools used are called interpreters.

To put it simply, a compiler is a “translation tool”, similar to translating Chinese into English and English into Russian. However, source code translation is a complex process, including lexical analysis, syntax analysis, semantic analysis, performance optimization, executable file generation and other five steps, involving complex algorithms and hardware architecture. The interpreter is similar, and for those interested, please refer to the Principles of Compilation.

Java and C# are a bit of an oddity. They are half-compiled, half-interpreted languages in which the source code is converted into an intermediate file (bytecode file), which is then taken to the virtual machine for execution. Java led the way, with its intent to be cross-platform and efficient; C# was a late follower, but C# stayed on Windows and had little success on other platforms.



Figure 1 Execution flow for compiled and interpreted languages

So what are the characteristics of compiled and interpreted languages? What’s the difference between them?

Compiled language

For compiled languages, all source code needs to be converted into an executable program after development, such as the.exe file under Windows, which contains the machine code. As long as we have an executable program, we can run it at any time without having to recompile it, which means “compile once, run it an infinite number of times.” At run time, we only need to compile the generated executable program, no longer need the source code and compiler, so compiled languages can be run independently of the development environment. Compiled languages are generally not cross-platform, meaning they cannot switch between different operating systems. Compiled languages are not cross-platform in two ways:

1) Executable programs cannot be cross-platform

It is easy to understand why executables are not cross-platform, because different operating systems have very different requirements for the internal structure of executables and are incompatible with each other. Not being cross-platform is natural, but being cross-platform is weird. For example, you can’t run Windows executables on Linux, and you can’t run Linux executables on Mac OS (even though they’re Unix-like systems). In addition, different versions of the same operating system may not be compatible, such as the inability to run X64 programs (Windows 64-bit programs) on x86 platforms (Windows 32-bit platforms). But the reverse is generally possible because 64-bit Windows is very compatible with 32-bit programs.

2) Source code cannot be cross-platform

Different platforms may support different functions, types, variables, etc., and source code written on one platform generally cannot be compiled on another. Let’s take C as an example. To pause a program in C, you can use the “Sleep” function, which is Sleep() on Windows and Sleep() on Linux. Second, Sleep() takes milliseconds, and Sleep() takes seconds, in different units. For these two reasons, pause C programs cannot be cross-platform unless compatibility is made at the code level, which is very troublesome. Although C languages on different platforms support the long type, the length of long varies from platform to platform. For example, long on Windows 64-bit occupies 4 bytes, while Long on Linux 64-bit occupies 8 bytes. Assigning 0x2F1e4AD23 to a variable of type long is perfectly fine when we write code on Linux 64-bit, but such an assignment would cause an overflow on Windows, causing the program to run incorrectly. Annoyingly, such errors are usually hard to detect because the compiler doesn’t report errors and we can’t remember the range of values for different types.

Interpretive language

For interpreted languages, each execution of the program needs to be executed as it is converted, and any source code that is not needed is converted to machine code without any processing. Different functionality may be used each time the program is executed, and the source code that needs to be converted is different. Because the source code needs to be re-converted every time a program is executed, interpreted languages are inherently less efficient than compiled languages, even by orders of magnitude. Some of the underlying functions of computers, or key algorithms, are generally implemented in C/C++, and interpreted languages are only used in applications (such as website development, batch processing, gadgets, etc.). When running an interpreted language, we always need source code and an interpreter, so it cannot be isolated from the development environment. When we say “download a program (software)”, different types of languages have different meanings:

  • For compiled languages, we download executable files and the source code is retained by the author, so programs in compiled languages are generally closed source.
  • For interpreted languages, we download all the source code, because the author can’t run it without the source code, so interpreted language programs are usually open source.

In contrast to compiled languages, interpreted languages are almost always cross-platform, and “write once, run anywhere” is true and ubiquitous. So why is interpreted language a faster platform? All thanks to the interpreter! When we say cross-platform, we mean source cross-platform, not interpreter cross-platform. The interpreter, which converts source code into machine code, is an executable program and is never cross-platform. Different interpreters need to be developed for different platforms. These interpreters must be able to follow the same syntax, recognize the same functions, and perform the same functions so that the same code will perform the same results on different platforms. You see, interpreted languages are cross-platform because of the interpreter middle layer. The interpreter translates the same source code into different machine code on different platforms, and the interpreter helps mask the differences between platforms.

About the Python

Python is typically an interpreted language, so running Python programs requires interpreter support. As long as you have different interpreters installed on different platforms, your code can be run at any time without worrying about compatibility issues, truly “write once, run anywhere.” Python supports almost all common platforms, such as Linux, Windows, Mac OS, Android, FreeBSD, Solaris, PocketPC, etc. You can write Python code to run correctly on these platforms without modification. That said, Python is very portable.

conclusion

The differences between compiled and interpreted languages are summarized in the following table:

type The principle of advantages disadvantages
Compiled language All source code is converted at once into machine code (in the form of executable files) for execution on a particular platform (Windows, Linux, etc.) through a specialized compiler. After compiling once, it can be run without the compiler and run efficiently. Poor portability and flexibility.
Interpretive language A special interpreter temporarily converts part of the source code to platform-specific machine code as needed. Good cross-platform, through different interpreters, the same source code will be interpreted into different platform machine code. It is inefficient to convert while executing.

supplement

The interpreter interprets the source language one by one (run as it is interpreted). PHP, Postscritp, and javascript are typical interpreted languages. It is inefficient to run, so some pre-compiled optimizations are usually done. The compiler compiles the whole source code into object code, and runs directly on the platform that supports object code without the need for the compiler. In this way, the execution efficiency is much faster than the interpretation. For example, C code is compiled into binary code (exe program) and executed on the Windows platform.

theyThe biggest difference is that while the program needs an interpreter to interpret and execute, the compiler doesn’t need one at all at runtime.

The advantage of the interpreter is that it is relatively easy for users to implement their own cross-platform code, such as Java, PHP, etc., the same set of code can be executed on almost all operating systems without modification according to the operating system; Is the purpose of the compiler to generate the target code generated by the connector again executable machine code, so need according to the different operating system code, although there is a source level such as Qt cross-platform programming tool library, but on different platforms still need to recompile connection into an executable file, but the performance is far higher than explain the running program.

Refer to the link

Compiler and interpreter C precompilation from variable promotion understanding precompilation is that simple, just play!!