Tag Archives: PyPy

Python vs PyPy installation and comparison (on Centos 6.x)

Python vs PyPy

in this article: Python vs PyPy I’ll explain how-to install both compilers (Python (CPython) and PyPy) on CentOS and the difference between them.

Requirements for examples:

  • Centos 6.x

 

Overview

The Download section of the Python official website (python.org) include list of several Alternative Implementations for python complier. some of them for Linux, some for Windows (.NET) and others.

I am going to review the default Python compiler vs PyPy on my CentOS 6.x machine.

 

python-logo

Python (nicknamed CPython)

Installation

yum install python

From Official website:

Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.

Python (nicknamed CPython) is the default Python Compiler you get with you installing Python.

Example

Create a file named ‘example.py‘ and fill it with:

print 'Hello, world!'

and type on bash:

python example.py

Note: you can also insert the following line as the first line in your .py file and then chmod +x this file to execute it as script:

#!/usr/lib/env python
print “Hello, world!”

and then on bash:

chmod +x example.py

and you can run the script (code) using:

./example.py

 

 

pypy-logo

PyPy

fast python implementation with a JIT compiler (generate native code on the fly) written in RPython and currently translated partly to C.
Arguments against PyPy compiled in Python are everywhere, but when C compiler gcc is implemented in C, is there any reason for the Python compiler to not be written in Python?

From Wikipedia:

PyPy is a Python interpreter and just-in-time compiler. PyPy focuses on speed, efficiency and compatibility with the original CPython interpreter.[1]

PyPy started out as a Python interpreter written in the Python language itself. Current PyPy versions are translated from RPython to C code and compiled. The PyPy JIT compiler is capable of turning Python code into machine code at run time.

 

Mission

The mission of PyPy is:

We aim to provide:

  • a common translation and support framework for producing implementations of dynamic languages, emphasizing a clean separation between language specification and implementation aspects. We call this the RPython toolchain.
  • a compliant, flexible and fast implementation of the Python Language which uses the above toolchain to enable new advanced high-level features without having to encode the low-level details. We call this PyPy.

By separating concerns in this way, our implementation of Python – and other dynamic languages – is able to automatically generate a Just-in-Time compiler for any dynamic language. It also allows a mix-and-match approach to implementation decisions, including many that have historically been outside of a user’s control, such as target platform, memory and threading models, garbage collection strategies, and optimizations applied, including whether or not to have a JIT in the first place.

 

How can it possibly beat CPython?

Manual memory management (which is what CPython does with its counting) can be slower than automatic management in some cases.

Limitations in the implementation of the CPython interpreter preclude certain optimisations that PyPy can do (eg. fine grained locks).

The JIT. Being able to on the fly confirm the type of an object can save you the need to do multiple pointer dereferences to finally arrive at the method you want to call.

 

Installation

yum install pypy

 

Using PyPy

To use pypy instead of your default CPython compiler you need to run your .py file with the pypy compiler.

to use pypy compiler you type at bash:

and type on bash:

pypy myapp.py

 

Summary

Python compiler (CPython) is the default and probably the safe choice so most python applications and modules will work; where PyPy can break some.

You should play with them both. Install them both on your CentOS and test / benchmark your applications using both compilers.

PyPy can rebust your web applcations (or any other code).

For example, Django app can be run on top of PyPy, with drawbacks.

 

Read more…