Joannah Nanjekye

Software Engineer, Aeronautical Engineer to be, Straight Outta 256 , I choose Results over Reasons, Passionate Aviator, Show me the code

I share my thoughts majorly here https://nanjekyejoannah.github.io/

Published

March 06, 2017

←Home

Faster python programs with pypy by Joannah Nanjekye

Pypy is an alternate implementation of the python programming language. Pypy started out as a python interpreter written in python. It is aimed at being compatible with cpython and currently with experimental compatibility for CPython C API extensions.

Pypy originally refered to two things. one the python interpreter and Rpython Translation toolchain but currently pypy is always used to refer to the python interpreter. The translation framework is always refered to as the Rpython Translation Framework.

It is the distinct features embedded in pypy that give your python programs the magical performance. Lets have a look at some of them.

Pypy Distinct Features

The pypy intepreter offers a couple of distinct features namely;

  • Speed
  • Compatibility
  • Memory Usage
  • Stackless python features

Speed

Pypy is magically faster due to its high performance Just-in-time compiler and garbage collector.

It is therefore faster for programs that are JIT susceptible. This means that pypy may not always run faster, it may be for pure python code but actually run slower than Cpython where the JIT cant help.

pypy may also not be as fast for small scripts that do not give the Just-in-time compiler enough warm up time.

I ran this code on pypy and on normal python and the results show pypy is actually faster. I will go on share the outcomes below;

Alot of benchmarks have shown pypy performing better than Cpython and its not getting slower with time. Each new version of pypy is faster than its predecessor. The pypy team have shared good insight at their speed center.

Compatibility

Good news is pypy is compatible with most of the python Libraries.

  • Django
  • Flask
  • Bottle
  • Pylons
  • Pyramid
  • Twisted
  • lxml
  • Beautiful Soup
  • Sphinx
  • IPyton
  • PIL/Pillow
  • Psycopg2
  • MySQL-Python
  • pysqlite
  • pymongo
  • cx_Oracle
  • SQLAlchemy
  • Gunicorn
  • Requests
  • nose
  • pytest
  • celery
  • Pip
  • Numpy
  • Scipy
  • Gevent

Pypy has Experimental support for Extension modules through cpyext. It can run most C extensions these days.

Memory Usage

Memory-intensive python programs take lesser space than when running on pypy . However this may not be the case always though due to some details.

Stackless python Features

Pypy exposes language features similar to the ones present in stackless python.

Differences between Cpython and Python

Lets take a look at pypy and a reference python implementation called cpython.

Way Forward

“If you want your code to run faster, you should probably just use PyPy.” — Guido van Rossum (creator of Python)

Installation

It is easy to install

Linux:-

`sudo apt-get install pypy`

Mac:-

`sudo brew install pypy`

Windows:-

There is rich Documentation on installation for windows.

Check these out for more pypy inspiration

Go Top