Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

INCREASING SPEED: CYTHON VS CPYTHON VS

PYTHON & PYPY

Last week I started this two-


part blog series about
increasing the speed of
Python through Cython
and PyPy. While we already
discussed the effects of
Cython, I wanted to
continue the discussion
with a look at PyPy and then a contrast of both PyPy and
Cython.
What is PyPy and how fast is it?
PyPy is an alternative Python interpreter and just-in-time (JIT)
compiler that is highly compatible (subject to a few caveats)
with the CPython interpreter. It is designed for speed and
efficiency and uses a tracing JIT compiler to optimize frequently
executed parts of the program at runtime, thus increasing the
execution speed.

PyPy runs regular Python programs; in general, no


modifications need to be made to Python code to allow it to run
on PyPy. Owing to the nature of the JIT compiler, it needs time
to analyze frequently executed parts of the program, so it works
best on programs that run for longer than a few seconds.

Perhaps the biggest hurdle to using PyPy in a production


system is its lack of support for CPython extension modules.
A di t th P P d t ti t i d l
According to the PyPy documentation, extension module
support is experimental and often runs much slower than in
CPython. This means that some third-party libraries that have C
extension modules might not work on PyPy — the scientific
computing library NumPy is a notable example. Having said
this, PyPy is able to run the majority of popular Python
frameworks and libraries, such as Django, Flask, and
SQLAlchemy.

The PyPy Wikipedia page and the PyPy website provide plenty
of information on what PyPy is and how to use it; the latter also
has a nice section that shows a list of benchmarks and their
speed improvements over CPython.

How Cython vs CPython Works


Test Program: Numerical Integration
My main objective in doing this exercise was to understand how
the tools work, and to be able to compare the differences in
syntax and code structure between them. With that in mind, I
chose to write a simple numerical integration program, which is
based on an example given in the Cython documentation. The
full project source, including installation and Cython build
instructions, can be found on Bitbucket.

In the following code listing, the pure Python implementation is


on the left, while the Cython version is on the right.

Cython vs Python Code


The Cython code differs from pure Python in the following ways:

Cython modules have a .pyx file extension instead of .py. The


Cython build process translates them into intermediate C
source files then compiles them using the system’s C compiler.

Cython code looks like Python code with added type


declarations. Type declarations for variables, function
arguments, and return values are optional, but adding them
gives Cython enough information to translate into more
g y g
optimized C code.

Cython allows easy linking to C libraries. At line 3, I’ve used the


cimport statement to import the native libc math library
instead of the Python math library, which improves
performance.

Cython allows native C functions, which have less overhead


than Python functions when they are called, and therefore
execute faster. At line 5, I’ve defined f as a native function using
the cdef statement. A downside to using native cdef functions is
that they are not accessible by Python code outside the Cython
module – effectively they are private.

Lines 9-11 in the integrate_f function define the variables.


Defining i as an integer at line 11 allows Cython to translate the
for loop at line 12 into a native C for loop, which improves
performance. If the type of i was not specified, the loop would
execute more slowly because the type of i would have to be
checked at each iteration.

CPython, Cython & PyPy Speed Results


Benchmark Results
I tested the following three implementations of the algorithm:

CPython: The pure Python implementation running in the standard


CPython interpreter
CPython + Cython: The Cython implementation running in the
standard CPython interpreter
PyPy: The pure Python implementation running in the PyPy
interpreter

In order to measure the speed of program execution for each


implementation, I wrote a program to call integrate_f 500 times
using an N value of 50,000, and recorded the execution time
over several runs.

I ran the tests on a 2015 Macbook Pro, using CPython 2.7.9,


Cython 0.24, GCC 4.2.1 and PyPy 2.5.1 (compatible with CPython
2.7.9).

The following table shows the benchmark results: Meet Your


Solution
Solution
Manager
Whether you
need expertise
in audio, video,
voice, IoT,
hardware,
The CPython + Cython implementation is the fastest; it is 44
embedded or
times faster than the CPython implementation. This is an
mobile
impressive speed improvement, especially considering that the
development,
Cython code is very close to the original Python code in its cloud or
design. quality
assurance, we
The PyPy implementation is 16 times faster than the CPython
can take your
implementation and about 3 times slower than the Cython
project from
implementation. This is fascinating since PyPy is running the
the initial
exact same pure Python code as the CPython implementation
concept to the
– it shows the power of PyPy’s JIT compiler. final market
Summary of Cython vs CPython vs PyPy launch.

And The Winner Is… Reach Out


I’ve enjoyed learning about Cython and PyPy and am Today
impressed by the speed gains that are possible with only a
small amount of programming effort. Speed is certainly
Recent Blog Posts
addictive, and it’s hard to go back to running my 9-second
Python program when I know the Cython one runs in 0.2 Domain-
seconds! Driven
Design in
My numerical integration test program is, of course, a toy
Embedded
problem, and it should be remembered that benchmarking Systems
results can vary considerably depending on the algorithms. In a Aug 03, 2022
real project, I would likely use NumPy or another specialized
third-party library to perform my calculations. How
Containeriz
CPython C extension modules, the Cython language and the Toolchains
PyPy interpreter are deep and interesting subjects in their own Improve
right and I’ve only scratched the surface in this blog post. Each Embedded
tool has its own pros and cons, and may be a great fit for one Software
project and a bad fit for another. Developme
May 11, 2022
Having experimented with both tools, I am more likely to use
Cython for speed-critical parts of programs in the future. The Why We
slow parts of the code can be identified by profiling and Use
refactoring into a Cython module, which could be written to TestRail
only contain the functions that needed to be optimized. for
Software
PyPy seems like it would be more of a quick fix, swapping out Testing
the CPython interpreter for PyPy without modifying a project’s May 06,
existing code base. I would be concerned, however, that 2021
somewhere down the line a third-party library would be Browse Blog
required for some functionality, and that it would turn out to be Topics

incompatible with PyPy. You could very possibly paint yourself Audio
into a corner. Automotive

I hope that I’ve managed to provide a valuable introduction to AWS


the world of Cython and PyPy. I already knew that Python was Cloud
awesome — now I know that it can be fast too! Transformation
Connected
If you enjoyed this blog and are curious about our job
Devices and IoT
openings, check out our careers page below.
Consumer
Health Care
Image
Are you a dedicated and innovative Processing
problem-solver? Networking
Outsourced
We’re always looking for people like you! Check out our Engineering
Careers page.
Product
Definition
Careers
Quality
Assurance
Security
Signal
Category: Software Development By Tom Craven Processing

Software
Author: Tom Craven Development
Streaming Media
Video
Voice

You might also like