当前位置:网站首页>The difference and working principle between compiler and interpreter

The difference and working principle between compiler and interpreter

2022-07-07 16:28:00 Le_ Sam

This tutorial , Let's learn how compilers and interpreters work , This is very important for us to improve our ability .

I always think that conceptual or theoretical knowledge is very important , Let's connect a lot of vague cognition , Become clearer , So as to open the field of vision , Rise to a new level .

however , Generally speaking , At the beginning , Touch some conceptual 、 Theoretical content , Not only is it very boring , And it's hard to understand .

And after a certain period of learning contact , Look at these things again , It will become easier to understand , Better understanding .

This tutorial will contain many technical terms , I will explain some of the technical terms , For some unexplained technical terms , It is recommended that you understand through search engines .

First , from Python Speaking of this programming language .

It has the following characteristics :

  • object-oriented : On this site 《Python3 Cute new entry notes 》 There are special articles in , Simply put, it means that encapsulation can be used in program design 、 Inherit 、 Polymorphic design method .
  • Dynamic language : Is a language whose structure can be changed at run time ; for example , While the program is running , Add a non-existent attribute to the object of a class .
  • Dynamic data type : Variables do not need to specify types , But the interpreter needs to identify the data type when executing code ; This feature makes programming simple , But code execution becomes less efficient .
  • High-level language : It refers to a highly encapsulated programming language , Compared to machine language , It is more suitable for human beings to write and read .
  • Explanatory language : It means that there is no need to compile , The language that can directly interpret the source code as machine language .

From the last feature , We can see Python It's interpreted language , That is to say, the source code needs to be interpreted and executed by the interpreter .

Programming languages are divided into compiled languages and interpreted languages , We need to understand the difference between them , To better understand the difference between compilers and interpreters .

I'm sure you all know that C and C++.

Both languages are compiled languages .

Compiled languages are characterized by fast execution , What are the disadvantages ?

Compiled languages require compiler processing , The main work flow is as follows :

Source code (source code) → The preprocessor (preprocessor) → compiler (compiler) → Object code (object code) → The linker (Linker) → Executable program (executables)

In this workflow , The compiler calls the preprocessor for related processing , Optimize and convert the source code ( Including clearing comments 、 Macro definition 、 Include files and conditional compilation ), then , By compiling the preprocessed source code into object code ( Binary machine language ), Then add the library file by calling the linker ( For example, the operating system provides API), So as to form an executable program , Enable the machine to perform .

In this workflow , The object code should be consistent with the machine CPU Architecture matching , The library file should match the operating system .

If you want to be different CPU Running on a machine or system C Language source code , You need to aim at different CPU Architecture and operating system , Only in this way can the program be run on the machine .

therefore , We can see the shortcomings of compiled languages , It is not suitable for cross platform .

and , You should know here , Why? CPU equally , however exe Program can only Windows Run in , But not in Mac Running on .

If it doesn't feel easy to understand , Let me give an example close to life :

A multilingual teacher taught many foreign students , These students are from England 、 The United States 、 The French 、 Germany 、 South Korea .

When the teacher handed out learning materials to these students , Need to translate Chinese materials first , Become an English version 、 German version 、 Electronic documents in French and Korean , And then send them to students in each country to study .

The translation work is very tedious .

Not only , To translate into the language of every country , and , We should also consider the differences between British English and American English and translate them into different English versions .

There's more , Every time the information is updated, you have to translate it again .

In the example above , Chinese teachers are like developers of compiled languages , Chinese materials are the source code of compiled languages , The translated materials are different CPU Machine language of architecture , Students of different languages are different CPU The machine of Architecture , American and British students , Namely CPU Machines with the same architecture but different operating systems .

Be careful : This involves the concept of cross platform , The platform can be understood as different CPU framework ( for example X86、ARM etc. ) Machines and the same kind CPU But different operating systems ( for example Unix、Windows etc. ) Machine .

Tips : I suggest you read the above , Take a look at the preprocessor 、 The linker 、 The library files ( Static link library and dynamic link library ) Related concepts of .

Let's continue with the example just now .

This tired Chinese teacher , Start thinking of ways .

He wants to , Why should I translate by myself , Just give them an automatic translation software ?

therefore , The teacher customized an automatic translation software for each student , This software can translate page by page original Chinese materials into different language materials for students to see .

The present situation , This teacher's work will be much easier , There is no need to consider making materials in various languages , Just focus on making Chinese materials .

Early interpreters were such workflows : Source code (source code) → Interpreter (interpreter) .

The source code does not need to be precompiled into an executable program .

When the program is executed , After the interpreter reads a sentence of source code , First of all, lexical analysis and grammatical analysis , The interpreter can convert the source code to the intermediate code ( Bytecode ), Last , The interpreter interprets the intermediate code into executable machine instructions .

therefore , The executable program of compiled language produces the direct execution of machine instructions , And every sentence of source code of interpretive language must be interpreted as executable machine instructions by the interpreter , In contrast, the execution efficiency of interpretive languages will be lower .

however , Interpretive languages have different interpreters on different platforms , The purpose of cross platform source code has been achieved , Developers no longer need to consider how to compile each platform , Just pay attention to the writing of the code , The finished code can be modified on any platform ( Or minor modifications ) You can do it right .

for example ,Linux Execute... In the system Python Source code support  fork() function , and window This function is not supported in the system , If it will run on Linux The source code in the system is ported to Windows System , At this time, it needs to be modified .

Understand the difference between compiled language and interpreted language , Let's continue with the example .

Although the teacher has customized translation software for students , But I found that the software was slow to translate every page , The reason is , This software needs to analyze every page first , Chinese with complex connotations ( For example, idioms ) Convert to simple and direct Chinese that can be translated directly , Then translate to other languages .

After seeing this problem , The software provider came up with a solution .

This plan is : The first time I open the data , Let the translation software analyze and transform the original data completely , Save it as an intermediate file that can be translated directly ; then , The translator reads the converted intermediate file page by page to translate ; In this case , Although it was a little slow when it was first opened , however , When the student opens the material again , As long as the original information is not updated , Translate directly through the saved intermediate file , There will be a big increase in speed . Of course , When the program opens , It is necessary to compare whether the original data is consistent with the intermediate documents , If there is any modification , Then compile a new intermediate file again , Overwrite the old intermediate file .

Python Program runtime , Like the example above , First convert the source code completely , Compile into more efficient bytecode , Save as suffix “.pyc” Bytecode file , then , The translator then translates the file sentence by sentence into machine language to execute .

Be careful :Shell Intermediate files will not be generated when executing source code in , Read the source code every time , After converting to bytecode , Explain to perform .

The above example is not over .

Although the solutions proposed by software suppliers solve some efficiency problems , But it is not completely satisfactory .

After much deliberation , Software vendors have come up with a new solution .

There are many repetitions in the original materials ;

If these repeated contents are translated once , Just save it , If you encounter the same content again, you can directly use the saved translation results .

There is no need to translate every time .

When running the program for a long time , The speed will be much faster .

This example is actually JIT Just in time compiler (Just-In-Time Compiler) Metaphor .

Whether using an interpreter to interpret or execute , Or use the compiler to compile and execute , Finally, the source code needs to be converted into the local machine instructions of the corresponding platform .

that , Some repeated code , It can be compiled into local machine instructions , Reuse , To improve efficiency .

These recurring codes include methods that are called many times and loop bodies that are executed many times .

JIT A typical example of an instant compiler is in JVM(Java virtual machine ) in .

Java The program is initially interpreted and executed through an interpreter , When Java When the virtual machine finds a method or code block running particularly frequently , Would think it was “ Hot code ”(Hot Spot Code).JIT The real-time compiler will put these “ Hot code ” Compile into machine instructions related to the local machine , Optimize at all levels .

When the program needs to be started and executed quickly , The interpreter works first , Save compilation time , Execute now . After the program runs , as time goes on , Compilers come into play , After compiling more and more code into local machine instructions , We can get higher execution efficiency . When the program running environment memory resource limit is large , You can use interpreter execution to save memory , On the contrary, we can use compile execution to improve efficiency .

Everybody knows ,Java The running performance of the program is very high , Basically, it can be with C/C++ Is comparable to . This is mainly because JIT Just in time compilers can target those that are frequently called “ Hot code ” Make deep optimization , Static compilers cannot infer exactly what is hot code at runtime , Instead of making targeted optimization . therefore , adopt JIT Only the local machine instructions compiled by the instant compiler have higher execution efficiency than the directly generated local machine instructions .

Python There are many kinds of interpreters , Well known are CPython、IPython、PyPy、Jython and IronPython etc. .

among CPython yes Python The official default interpreter , It is to use C Language implementation Pyhon Interpreter .

CPython It's a simple interpreter , After converting the source code into bytecode, explain and execute .

And the other one uses Python Realized Python Interpreter PyPy, Than CPython The interpreter is more flexible . because PyPy Adopted JIT technology , In the running performance of the program PyPy Almost CPython Interpreter execution efficiency 1 to 5 times .

Other interpreters have their own characteristics .

IPython Is based on CPython Enhanced interaction .

Jython Is running on the Java On the platform Python Interpreter .

IronPython Is running on the .Net On the platform Python Interpreter .

The above is the difference and working principle between compiler and interpreter , Because there is no ready-made information , therefore , The content refers to a lot of online materials , After many checks , It's fused . If there is an interpretation error , Or where the explanation is not thorough , You are welcome to make corrections and suggestions .

原网站

版权声明
本文为[Le_ Sam]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130609270423.html