Execution of Python Program :
-----------------------------1. Python program with name welcome.py, Here welcome is the program name and .py is extension.
2. After typing the program next step is to compile the program using python compiler.
source code ----------> Byte code
(compiler) (.pyc)
3. The compiler can converts Python program in to byte code.
4. Byte code(.pyc) represents fixed set of instructions that represents all operations like
arithmetic operations, comparison operation and memory operations etc. which run on any
operating system and Hard ware.
It means Byte code is system independent or platform independent.
5. The generated Byte code can not understand by computer. It understands only binary code(0's,1's) called machine code.
So it is necessary to convert Byte code to machine code. For this purpose, We should use PVM(Python Virtual Machine)
Byte code ----------------> Machine code
(PVM)
6. PVM uses interpreter which understands the byte code and converts machine code.
7. The machine code instructions then executed by the processor and results will displayed.
Normally when we compile a python we cannot see .pyc file produced by python compiler and machine code generated by PVM
This is done internally in the memory and the output is finally visible.
if our python program name is welcome.py we can use python compiler to compiles it as :
C:>python welcome.py
Here python is a command to calling the Python compiler
NOTE : When ever executing provide complete path of .py .
The compiler should convert the welcome.py file into byte code equivalent file, welcome.pyc. Instead of doing this, compiler
directly displays the output or result.
Here sourcecode ---> bytecode---> machinecode are internally completed and then final result is displayed. Hence after
executeion , we can not find any .pyc file in directory
To separately create .pyc file from source code, we can use following command.
C:\> python -m py_compile welcome.py
Here -m represents module and module name is py_compile
This module generate .pyc file for specified .py file. Here Compiler create separate Directory by the name __pycache__ which stores generated .pyc file (welcome.cpython-36.pyc)
C:\> python welcome.cpython-36.pyc
In this case, we are supplying .pyc file to Python compiler. Now python compiler will skip the step where it has to convert source code into byte code
as already it sees byte code inside this .pyc file. So this file is directly by the PVM to produce output.
Hence, The program take less time to run and performance will improved.
Viewing Byte code
-------------------
addition.py
a=10
b=20
sum=a+b
print(sum)
C:\> python -m dis addition.py
C:\Users\welcome\Desktop\Python>python -m dis addition.py
1 0 LOAD_CONST 0 (10)
2 STORE_NAME 0 (a)
2 4 LOAD_CONST 1 (20)
6 STORE_NAME 1 (b)
3 8 LOAD_NAME 0 (a)
10 LOAD_NAME 1 (b)
12 BINARY_ADD
14 STORE_NAME 2 (sum)
4 16 LOAD_NAME 3 (print)
18 LOAD_NAME 2 (sum)
20 CALL_FUNCTION 1
22 POP_TOP
24 LOAD_CONST 2 (None)
26 RETURN_VALUE
Here byte code is displayed by dis module which is also known as 'disassembler' that displays the byte code in human understandable format.
Above output you can find 5 columns
1. line number (Left-most column)
2. Offset position of bytecode
3. Name of byte code instruction (LOAD_CONST,STORE_NAME etc)
4. Number of arguments for instruction ( Eg: for sum we are passing 2 arguments so value 2)
5. Names specified by the 4th column
PVM( Python Virtual Machine)
-------------------------------
* The standard PVM contains only interpreter and hence python is called as interpreter language.
* PVM is most often called Python interpreter
* Then PVM of PyPy contains compiler in addition to the interpreter.
This compiler is called JIT compiler(Just In Time) which is user to speed up the execution
of python program.
* The programmers need not allocate or deallocate memory in python. It is duty of PVM
to allocate or deallocate memory for python programs.
* Memory manager is a module(or subprogram) in PVM which will allocate memory for objects.
* Garbage collector is another module in PVM that will deallocate(or free) memory for unused objects.
No comments:
Post a Comment