Python – Program execution.

This post is the second of many that attempts to document everything I have been learning about Python.

Python code files a.k.a Modules

Python files end with the .py extension. This is not required, only files that have to be imported have to end with .py. Most python script files follow this convention.

Program Execution

When you run the a python script, the source code is first compiled to byte code, if Python has write access to the machine, it will generate this byte code and save it in .pyc file, else, it will just store it in memory. Byte code is low level portable code that the python interpreter can execute with greater speed than the source code.

When you run the same program again, unless the source file has been changed, the interpreter just runs the byte code in the .pyc file. This can result in improvement in startup speed, as python does not have to do a compile step.

Python can run programs just using .pyc. It does not need to have the .py source file.

The Python Virtual Machine then runs through the byte code file, executing each instruction from top to bottom.

To you, the programmer, this process consists of entirely one step, running the code file with the interpreter. The compilation step to generate the byte code and the execution of the byte code by PVM happens behind the scenes.

Because the PVM has to interpret the byte code instructions and run them, Python code usually runs slower than C and C++ code which are always compiled to machine code and executed directly by the CPU.

Implementations of Python

Python language has 3 implementations.

  1. CPython: This is the standard implementation. It is written using ANSI C
  2. Jython: This implementation allows Python programs to inherit and call programs written in Java. Jython converts Python source code to java byte code and runs it on the JVM.
  3. IronPython: Similar to Jython, except this compiles .py to .net IL code and then executes it on the CLR.

Some tools that speed up program execution

Psyco JIT:

As a python program is being executed, Psyco will attempt to convert the data types being used to native binary code. This can lead to some decent improvements in speed. It attempts to read and understand the types being used in the program. It then compiles those types to native machine code. This machine code can then replace parts of the python byte code which leads to faster execution.

Psyco has to be installed separately, it is not part of the standard Python install.

Currently, it only generates machine code for x86 chips.

Shedskin C++ compiler:

Attempts to translate python source code into C++ and execute the resulting C++ code using the C++ compiler. This dictates that Python programs be written using what can be described as a non-pythonic approach.

Frozen binaries

You can package python byte code files, PVM and support files in a single exe that can be shipped off to consumers.

Tools that help you create frozen binaries:

Other execution options

Stackless Python: Does not save state data on the C stack.

Cython: Hybrid language which allows you to use C type declarations in python code and call C functions. Cython code can be compiled to C code that uses the Python / C API. It is not completely compatible with standard Python

Parrot: Attempts to provide a common byte code format and virtual machine environment for different languages

PyPy: Attempt to re-write PVM using Python

Unladen Swallow: A branch of Cpython that is being worked on to make Python run up to 5 times faster.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s