Python interpreter and bytecode compilation __python

Source: Internet
Author: User

A. Python interpreter

Python is a programming language and a software package for interpreters. An interpreter is a program that lets other programs run. When you write a Python program, the Python interpreter reads the program and executes it in accordance with the commands in it, resulting in the results. The interpreter is the software logic layer between the code and the computer hardware.

When the Python package is installed, it contains some minimized components: an interpreter and a supported library. Depending on the usage, the Python interpreter may take the form of an executable program, or as a series of libraries linked to another program. Depending on the Python version used, the interpreter itself can be implemented with a C program, or some Java class implementations, or some other form. Either way, the Python code you write must run in the interpreter, so to run Python, you must first install the Python interpreter.

Two. Python Program execution Process

2.1-byte Code compilation

When a program executes, the Python interior (which is completely hidden from most users) first compiles the source code into a byte-code form. Compilation is a simple translation process, and bytecode is the source code low-level, platform-independent representation. In a nutshell, Python translates these source statements into a set of byte-code directives by decomposing each source statement into a single step. These bytecode can improve execution speed, and bytecode runs much faster than the original code statements in the text file.

The translation process is hidden from the user, and if the Python process has write permission on the machine, it saves the program's bytecode as a file with a. pyc extension, and the PYc file is the compiled py source file. When the program is run, the PYc files are viewed in the same directory.

Python saves bytecode as a startup speed optimization. The next time you run the program, if the source code has not been modified since the last time the byte code was saved, Python will load the PYc file and skip compiling this step. When Pyhon must recompile, it automatically checks the timestamp of the source file, and if the source code is saved, the bytecode is automatically recreated the next time the program runs.

If Python cannot write bytecode on the machine, the program can still work, at which point the bytecode will be generated in memory and simply discarded at the end of the program. Despite this, because the PYc file can be accelerated to start, for large programs, you should give Python write permission, if you save the PYc file, you can speed up the program.

2.2 Python Virtual Machine (PVM)

Once the program is compiled into bytecode or the bytecode is loaded from an existing PYc file, the bytecode is sent to the Python virtual machine (Machine) for execution. PVM is a large loop that iterates through the bytecode instructions, and completes the operation one after another. PVM is Python's running engine, which is often represented as part of a Python system, and it is the component that actually runs the script. This step is the final step of the Python interpreter.

2.3 Performance

A fully-compiled language such as C and C + + requires build or make, but Python bytecode is not a machine's binary code and does not require making steps.

This is why Python code cannot run as fast as C or C + + code, because the PVM loop still needs to interpret bytecode, and bytecode directives require more work than CPU instructions. Unlike other classic interpreters, there is still an internal compilation step: Python does not need to repeatedly parse and decompose each line of statements, and the real effect is that pure Python code runs at speeds between traditional and traditional interpretation languages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.