• notice
  • Congratulations on the launch of the Sought Tech site

The difference between compiled language and interpreted language

The source code we write is in human language and we can easily understand it ourselves; but for computer hardware (CPU), the source code is a heavenly book and cannot be executed at all.The computer can only recognize certain specific binary instructions.The source code is converted into binary instructions.

The so-called binary instructions, that is, machine code, are hardware-level "codes" that the CPU can recognize.Simple hardware (such as ancient single-chip computers) can only use dozens of instructions, and powerful hardware (PCs and smartphones) can use them.Hundreds of instructions.

However, when exactly will the source code be converted into binary instructions? Different programming languages have different regulations:

  1. Some programming languages require that all source codes must be converted into binary instructions 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 a compiled language, and the conversion tool used is called a compiler.

  2. Some programming languages can be converted while executing, and the source codes are converted as needed, and executable programs are not generated, such as Python, JavaScript, PHP, Shell, MATLAB, etc.This programming language is called an interpreted language and uses The conversion tool is called an interpreter.

Simply understand, the compiler is a "translation tool", similar to translating Chinese into English and English into Russian.However, translating source code is a complex process, which roughly includes five steps: lexical analysis, syntax analysis, semantic analysis, performance optimization, and executable file generation, involving complex algorithms and hardware architecture.The interpreter is similar to this, interested readers please refer to the book "Compiler Principles", this article will not repeat them.

Java and C# are a kind of strange existence.They are semi-compiled and semi-interpreted languages.The source code needs to be converted into an intermediate file (bytecode file), and then the intermediate file is executed in the virtual machine.Java led this trend.Its original intention was to take into account execution efficiency while being cross-platform; C# was a later follower, but C# has always stopped on the Windows platform and has little effect on other platforms.

1-1912311J415L7.gif

Figure 1 The execution flow of compiled language and interpreted language

So, what are the characteristics of compiled languages and interpreted languages? What is the difference between them?

Compiled language

For compiled languages, after the development is completed, all source codes need to be converted into executable programs, such as.exe files under Windows.The executable program contains machine code.As long as we have an executable program, we can run it at any time without recompiling, that is, "compile once, run unlimited times".

When running, we only need to compile the generated executable program, no longer need the source code and compiler, so that the compiled language can run out of the development environment.

Compiled languages are generally not cross-platform, that is, they cannot be switched between different operating systems at will.

Compiled languages cannot be cross-platform in two ways:

1) Executable programs cannot be cross-platform

Executable programs cannot be easily understood across platforms, because different operating systems have completely different requirements for the internal structure of executable files, and they are not compatible with each other.Not being able to cross-platform is justified, but being able to cross-platform is a wonderful thing.

For example, you cannot use executable programs under Windows under Linux, and you cannot use executable programs under Linux under Mac OS (although they are all Unix-like systems).

In addition, different versions of the same operating system are not necessarily compatible, for example, x64 programs (Windows 64-bit programs) cannot be run on x86 platforms (Windows 32-bit platforms).But the opposite is generally feasible, because 64-bit Windows has made a good compatibility treatment for 32-bit programs.

2) The source code cannot be cross-platform

The functions, types, variables, etc.supported by different platforms may be different, and the source code written on one platform generally cannot be compiled under another platform.Let's take C language as an example.

[Example 1] In C language, if you want to pause the program, you can use the "sleep" function.The function is Sleep() on the Windows platform, and sleep() on the Linux platform.The case of the first letter is different.Secondly, the parameter of Sleep() is milliseconds, the parameter of sleep() is seconds, and the unit is different.

The above two reasons cause the C language program that uses the pause function to not be cross-platform, unless compatibility is handled at the code level, which is very troublesome.

[Example 2] Although the C language of different platforms supports the long type, the length of the long of different platforms is different.For example, the long under the Windows 64-bit platform occupies 4 bytes, and the long under the Linux 64-bit platform occupies 8 byte.

When we write code under Linux 64-bit platform, there is no problem at all to assign 0x2f1e4ad23 to long type variable, but such assignment will cause numerical overflow under Windows platform, causing the program to produce wrong results.

It is distressing that such errors are generally not easy to detect, because the compiler will not report errors, and we can't remember the different types of value ranges.

Interpreted language

For interpreted languages, every time the program is executed, it needs to be converted and executed.The source code is converted into machine code if it is used, and no processing is performed on the unused source code.Different functions may be used each time the program is executed, and the source code that needs to be converted is different at this time.

Because the source code needs to be re-transformed every time the program is executed, the execution efficiency of interpreted languages is inherently lower than that of compiled languages, and there is even an order of magnitude gap.Some low-level computer functions, or key algorithms, are generally implemented in C/C++, and interpreted languages are only used at the application level (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 separated from the development environment.

When we say "download a program (software)", different types of languages have different meanings:

For compiled languages, what we download is an executable file, and the source code is kept by the author, so programs in compiled languages are generally closed source.

For interpreted languages, what we download is all the source code, because the author cannot run without the source code, so the programs in interpreted languages are generally open source.

Compared with compiled languages, interpreted languages can almost be cross-platform."Write once, run everywhere" really exists, and it's everywhere.So, why can interpreted languages be faster than platforms?

All this is due to the interpreter!

When we talk about cross-platform, we mean cross-platform source code, not cross-platform interpreter.The interpreter is used to convert source code into machine code.It is an executable program and is absolutely not cross-platform.

Officials need to develop different interpreters for different platforms.These interpreters must be able to abide by the same syntax, recognize the same functions, and perform the same functions.Only in this way, the execution results of the same code on different platforms are the same.

You see, the reason why interpreted languages can be cross-platform is because of the middle layer of the interpreter.Under different platforms, the interpreter will convert the same source code into different machine codes.The interpreter helps us shield the differences between different platforms.

About Python

Python is a typical interpreted language, so running Python programs requires the support of an interpreter.As long as you install different interpreters on different platforms, your code can run at any time without worrying about any compatibility issues.Write, run everywhere".

Python supports almost all common platforms, such as Linux, Windows, Mac OS, Android, FreeBSD, Solaris, PocketPC, etc.The Python code you write can run correctly on these platforms without modification.In other words, Python's portability is very strong.

to sum up

We summarize the differences between compiled languages and interpreted languages in the following:

Type Principle Advantage Disadvantage

Compiled language Through a special compiler, all source codes are converted into machine code (in the form of executable files) for execution on a specific platform (Windows, Linux, etc.) at one time.After compiling once, it can run without the compiler, and the running efficiency is high.Poor portability and not flexible enough.

Interpreted language A special interpreter temporarily converts part of the source code into machine code for a specific platform as needed.Good cross-platform performance, through different interpreters, interpret the same source code into machine code under different platforms.Performing while converting, the efficiency is very low.

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+