current position:Home>The difference between compiled language and interpreted language

The difference between compiled language and interpreted language

2021-08-27 10:30:57 JR

The source code we write is human language , We can easily understand ; But for computer hardware (CPU), The source code is the book of heaven , It can't be executed at all , A computer can only recognize certain binary instructions , The source code must be converted to binary instructions before the program can actually run .

So called binary instructions , That's machine code , yes CPU The hardware level that can be identified “ Code ”, Crude hardware ( For example, the old single chip microcomputer ) Only a few dozen instructions can be used , Powerful hardware (PC And smartphones ) Can use hundreds of instructions .

However , When to convert source code to binary instructions ? Different programming languages have different rules :

  • Some programming languages require that all source code must be converted into binary instructions at one time in advance , That is to generate an executable program (Windows Under the .exe), such as C Language 、C++、Golang、Pascal(Delphi)、 Assembly, etc , This programming language is called a compiler language , The conversion tool used is called a compiler .
  • Some programming languages can transform while executing , Convert whatever source code you need , No executable program will be generated , such as  PythonJavaScriptPHP、Shell、MATLAB  etc. , This programming language is called interpretive language , The conversion tool used is called an interpreter .


Simple understanding , A compiler is just a “ translation tool ”, It's similar to translating Chinese into English 、 Translate English into Russian . however , Translating source code is a complex process , It includes lexical analysis 、 Syntax analysis 、 Semantic analysis 、 performance optimization 、 There are five steps to generate an executable file , It involves complex algorithm and hardware architecture . The interpreter is similar to this , Interested readers please refer to 《 Compiler principle 》 A Book , This article will not be repeated .

Java  and  C#  It's a wonderful existence , They are semi compiled, semi interpreted languages , The source code needs to be converted into an intermediate file ( Bytecode file ), Then take the intermediate file to the virtual machine for execution .Java Leading this trend , Its original intention is to achieve cross platform execution efficiency ;C# It's a follower later , however C# Stop at Windows platform , There's little to be done on other platforms .

 The execution flow of compiler language and interpretive language
chart 1  The execution flow of compiler language and interpretive language


that , What are the characteristics of compiler language and interpretive language ? What's the difference between them ?

Compiler language

For compiled languages , After the development is completed, all the source code needs to be converted into executable program , such as Windows Under the .exe file , The executable program contains the machine code . As long as we have executable programs , You can run at any time , There's no need to recompile , That is to say “ A compilation , Infinite runs ”.

During operation , We just need to compile the generated executable , No more source code and compiler needed , So compiler language can run without development environment .

Compiler languages are generally not cross platform , That is, you can't switch between different operating systems .

Compiled languages can't cross platforms in two ways :

1) Executable programs cannot cross platforms

Executable programs are not cross platform and easy to understand , Because different operating systems have different requirements for the internal structure of executable files , They're not compatible with each other . It's natural that we can't cross platforms , It's wonderful to be able to cross platforms .

such as , Can't be Windows Under the executable program to get Linux Next use , We can't Linux Under the executable program to get Mac OS Next use ( Although they are all class Unix System ).

in addition , Different versions of the same operating system are not necessarily compatible , For example, you can't put x64 Program (Windows 64 Bit program ) Get x86 platform (Windows 32 Bit platform ) Run under . But the opposite is generally possible , because 64 position Windows Yes 32 Bit program has done a good compatibility processing .

2) Source code cannot cross platform

Functions supported by different platforms 、 type 、 Variables and so on can be different , Source code written on one platform can't be compiled on another platform . We use C Language as an example to illustrate .

【 example 1】 stay C Language to make the program pause can be used “ sleep ” function , stay Windows Under the platform, the function is Sleep(), stay Linux Under the platform, the function is sleep(), The initial case is different . secondly ,Sleep() The parameter for is milliseconds ,sleep() The parameter for is seconds , Units are different too .

The above two reasons lead to the use of pause function C Language programs can't cross platforms , Unless you do compatibility at the code level , Very trouble .

【 example 2】 Although different platforms C All languages support long type , But different platforms long But the length of it is different , for example ,Windows 64 On a bit platform long Occupy 4 Bytes ,Linux 64 On a bit platform long Occupy 8 Bytes .

We are Linux 64 When writing code on bit platform , take 0x2f1e4ad23 Assign a value to long There is no problem with variables of type , But this kind of assignment is in Windows It will lead to value overflow under the platform , Let the program produce the wrong result .

Distressing , Such mistakes are not easy to detect , Because the compiler doesn't report errors , We can't remember the different types of ranges .

Explanatory language

For explanatory language , Every time the program is executed, it needs to be converted and executed at the same time , The source code used will be converted into machine code , Don't deal with anything you can't use . Different functions may be used each time the program is executed , At this time, the source code to be converted is not the same .

Because every time you execute the program, you need to convert the source code again , So interpretive languages are inherently less efficient than compiled languages , There is even an order of magnitude gap . Some of the underlying functions of computers , Or the key algorithm , Generally used C/C++ Realization , Only at the application level ( For example, website development 、 The batch 、 Small tools, etc ) To use explanatory language .

When you run an interpreted language , We always need source code and an interpreter , So it can't be separated from the development environment .

When we say that “ Download a program ( Software )” when , Different types of languages have different meanings :

  • For compiled languages , What we downloaded is an executable file , The source code is reserved by the author , So compiler language programs are generally closed source .
  • For explanatory language , We download all the source code , Because the author can't run without the source code , So interpretive language programs are generally open source .


Compared with compiled languages , Almost all interpretive languages can cross platform ,“ Write once , Run anywhere ” It really exists , And it's everywhere . that , Why can interpretive language speed up the platform ?

Thanks to the interpreter !

What we call cross platform , Cross platform source code , Instead of interpreter cross platform . The interpreter is used to convert source code into machine code , It's just an executable program , It is absolutely not cross platform .

Officials need to develop different interpreters for different platforms , These interpreters have to be able to follow the same syntax , Identify the same function , Perform the same function , That's the only way , Only when the same code is executed on different platforms will the result be the same .

You see , The reason why explanatory languages can cross platform , Because of the middle layer of the interpreter . On different platforms , The interpreter will convert the same source code into different machine code , The interpreter helps us shield the differences between different platforms .

About Python

Python It is a typical explanatory language , So run Python  Programs need interpreter support , As long as you have different interpreters installed on different platforms , Your code can run at any time , Don't worry about any compatibility issues , real “ Write once , Run anywhere ”.

Python Supports almost all common platforms , such as Linux、Windows、Mac OS、Android、FreeBSD、Solaris、PocketPC etc. , What you wrote Python Code can run correctly on these platforms without modification . in other words ,Python Portability is very strong .

summary

We summarize the differences between compiled and interpreted languages as follows :

type principle advantage shortcoming
Compiler language Through a special compiler , Convert all source code to a specific platform at one time (Windows、Linux etc. ) Machine code executed ( In the form of an executable ). After compiling once , You can run without a compiler , And the operation efficiency is high . Poor portability , inflexible .
Explanatory language By a special interpreter , Temporarily convert part of the source code to the machine code of a specific platform as needed . Good cross platform , Through different interpreters , Interpret the same source code into machine code under different platforms . Switch while executing , Efficiency is very low .

Add

Interpreter It is the source language of interpretation and execution one by one ( Run as you explain ). such as php,postscritp,javascript It's a typical explanatory language . Low operating efficiency , So we usually do some Precompiled optimization .
compiler Is to compile the whole source code into object code , The compiler is no longer required for execution , Run directly on a platform that supports object code , This is more efficient than the explanation Fast execution quite a lot . such as C The language code is compiled into binary code (exe Program ), stay windows Execution on the platform .

image.png they The biggest difference is that when the program runs, the interpreter needs to interpret and execute , The compiler is completely unnecessary at run time .

The advantage of the interpreter is that it is easier 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 , There is no need to modify the operating system ; The purpose of the compiler is to generate object code, and then the connector generates executable machine code , In this case, you need to code according to different operating systems , Although it's like Qt Such a source code level cross platform programming tool library , But on different platforms, you still need to recompile and connect to executable files , But its execution efficiency is much higher than that of interpreting the running program .

Reference link

The difference between compiled and interpreted languages
The difference between compiler and interpreter
C Language precompiling
Understand precompiling from variable promotion , It's that simple , Just play !!

copyright notice
author[JR],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827103055194b.html

Random recommended