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 .
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 .
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 ？
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 .
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 .
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 .
We summarize the differences between compiled and interpreted languages as follows ：
|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 .|
Interpreter It is the source language of interpretation and execution one by one (
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 .
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 .
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 ！！
author[JR],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Crazy blessing! Tencent boss's "million JVM learning notes", real topic of Huawei Java interview 2020-2021
- How to implement injection in vuex source code?
- JQuery operation select (value, setting, selected)
- An article disassembles the pyramid architecture behind the gamefi outbreak
- BEM - a front-end CSS naming methodology
- [vue3] encapsulate custom global plug-ins
- Error using swiper plug-in in Vue
- Another ruthless character fell by 40000, which was "more beautiful" than Passat and maiteng, and didn't lose BMW
guess what you like
Huang Lei basks in Zhang Yixing's album, and the relationship between teachers and apprentices is no less than that in the past. Netizens envy Huang Lei
He was cheated by Wang Xiaofei and Li Chengxuan successively. Is an Yixuan a blessed daughter and not a blessed home?
Zhou Shen sang the theme song of the film "summer friends and sunny days" in mainland China. Netizen: endless aftertaste
Pink is Wangyuan online! Back to the peak! The new hairstyle is creamy and sassy
Front end interview daily 3 + 1 - day 858
Spring Webflux tutorial: how to build reactive web applications
[golang] walk into go language lesson 24 TCP high-level operation
August 23, 2021 Daily: less than three years after its establishment, Google dissolved the health department
The female doctor of Southeast University is no less beautiful than the female star. She has been married four times, and her personal experience has been controversial
There are many potential safety hazards in Chinese restaurant. The top of the program recording shed collapses, and the artist will fall down if he is careless
- Anti Mafia storm: He Yun's helpless son, Sun Xing, is destined to be caught by his dry son
- Introduction to flex flexible layout in CSS -- learning notes
- CSS learning notes - Flex layout (Ruan Yifeng tutorial summary)
- Today, let's talk about the arrow function of ES6
- Some thoughts on small program development
- Talk about mobile terminal adaptation
- Unwilling to cooperate with Wang Yibo again, Zhao Liying's fans went on a collective strike and made a public apology in less than a day
- JS function scope, closure, let, const
- Zheng Shuang's 30th birthday is deserted. Chen Jia has been sending blessings for ten years. Is it really just forgetting to make friends?
- Unveil the mystery of ascension
- Asynchronous solution async await
- Analysis and expansion of Vue infinite scroll source code
- Compression webpack plugin first screen loading optimization
- Specific usage of vue3 video play plug-in
- "The story of huiyeji" -- people are always greedy, and fairies should be spotless!
- Installing Vue devtool for chrome and Firefox
- Basic usage of JS object
- Two easy-to-use animation JS that make the page move
- Front end Engineering - scaffold
- Java SQL Server intelligent fixed asset management, back end + front end + mobile end
- Array de duplication problem solution - Nan recognition problem
- New choice for app development: building mobile applications using Vue native
- New gs8 Chengdu auto show announces interior Toyota technology blessing
- Vieira officially terminated his contract and left the team. The national security club sent blessings to him
- Less than 200000 to buy a Ford RV? 2.0T gasoline / diesel power, horizontal bed / longitudinal bed layout can be selected
- How does "heart 4" come to an end? Pinhole was boycotted by the brand, Ma Dong deleted the bad comments, and no one blessed him
- We are fearless in epidemic prevention and control -- pay tribute to the front-line workers of epidemic prevention!
- Front end, netty framework tutorial
- Xiaomi 11 | miui12.5 | android11 solves the problem that the httpcanary certificate cannot be installed
- The wireless charging of SAIC Roewe rx5 plus is so easy to use!
-  typescript
- CSS transform Complete Guide (Second Edition) flight.archives 007
- Ajax foundation - HTTP foundation of interview essential knowledge
- Cloud lesson | explain in detail how Huawei cloud exclusive load balancing charges
- [JS] 10. Closure application (loop processing)
- Left hand IRR, right hand NPV, master the password of getting rich