paint-brush
Which is the fastest version of Python?by@anthonypjshaw
68,069 reads
68,069 reads

Which is the fastest version of Python?

by Anthony ShawMarch 30th, 2018
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Of course, “it depends”, but what does it depend on and how can you assess which is the fastest version of Python for your application?

Company Mentioned

Mention Thumbnail
featured image - Which is the fastest version of Python?
Anthony Shaw HackerNoon profile picture

Of course, “it depends”, but what does it depend on and how can you assess which is the fastest version of Python for your application?

Is Python 3 slower than Python 2? Which version of Python 3 is the fastest and what other options do you have for speed?

Using the performance suite utility

The core Python team care a lot about performance, I’ve mentioned before the speed.python.org website, which is great to compare the “official” benchmarks against versions of CPython.

There are a couple of problems though:

  1. The results are quite hard to read
  2. They don’t include PyPy

You can instead download the toolbox that runs this website by runningpip install performance then you can run

pyperformance run --python={chosen_python_runtime} -o my_results.json

This will run a series of documented “real world” applications against that target version of Python multiple times and record the mean, median and

This is effectively what I’ve done for this article, against the official installations of Python:

  • 2.7.10,
  • 3.4.4,
  • 3.5.4,
  • 3.6.1, and
  • 3.7 (I have beta 2).

Also, this includes PyPy(5.6.) and PyPy3 (5.4.10).

The results

I’ve broken the results down depending on the use case, so take a look, run your own test and possibly write your own test.

I also created a simple script to take a list of perf data files and create graphs for each test. The code is on GitHub.

In all graphs, the results are in seconds and lower is better.

The full results along with the graphs are available here: https://github.com/tonybaloney/performance_testing/tree/master/png

I have included the tests which I though were of significance. The rest of the tests show very similar patterns, which are in the conclusion.

Rendering HTML templates

The django_html test will use the Django template rendering engine to to build a 150x150-cell HTML table. It leverages the Content and Template classes of the Django engine.

Python 3.7 is 1.19x faster than Python 2.7, but the only Python 3.x release to beat the Python 2.7 benchmark I ran. The speed.python.org benchmark shows similar results.

PyPy smashes any of the CPython results, but with PyPy3 twice as slow as PyPy. Worth noting is the recent decision by Django to drop Python 2 support in Django 2.0 and beyond, which means PyPy would also no longer be compatible with Django 2.

Startup time

This test simply tests the time taken for the interpreter to start. If you’re getting around Python’s “GIL” constraint by running multiple processes then this will be significant.

Take note here about the jump with PyPy, especially PyPy3, I’ll go into why at the end of this article.

But the crux of this is that Python 2.7 startup time is still unbeaten.

Cryptography: crypto_paes

In this test you’ll see a notable drop in speed between Python 2 and 3. Why? Crypto requires a lot of number crunching and Python 3 no longer has a 32-bit integer type, only a (very) long integer.

PyPy users — you’ll notice that PyPy3 is almost 5x slower than PyPy!

n-queens: the algorithm test

This implementation isn’t particularly elegant, and sorry for bringing up bad memories for anyone who had to sit through Algorithms lectures on this topic. The puzzle is quite simply to place (n) Queens on a chess board so that they can’t take each other.

Of the CPython series, 3.7 comes out top again, but notably PyPy and PyPy3 results are very similar.

Floating point arithmetic

The “float” benchmark is an artificial, floating point arithmetic heavy application that will create 100,000 point objects which compute math.cos(), math.sin() andmath.sqrt() .

This is the kind of application that is perfect for PyPy, lots of number crunching, predictable types and methods and looping. Python 3.7 has the new fast-method calling opcode which is being utilised on this test.

Regular expressions

In the regex test, “50 of the most popular pages on the web and logging all regexp operations performed. Each operation is given a weight that is calculated from an estimate of the popularity of the pages where it occurs and the number of times it is executed while loading each page. Finally the literal letters in the data are encoded using ROT13 in a way that does not affect how the regexps match their input.”

I don’t know what happened to PyPy here, I’d be curious to know if anyone else has the same result!

Update: the PyPy team saw this article and fixed the issue in a number of hours

So is Python 3 faster than Python 2?

Yes! in almost all tests. The notable exceptions were the crypto_paes test, where Python 3 was 1.35x slower (because of the integer types), python_startup as 1.39x slower.

The slow Python 3 startup is among the issues the core CPython team are working on for 3.8, 3.9 releases.

Aside from those 2 tests, Python 3 is around 1.2–1.3x faster in these benchmarks. You should see an improvement upgrading to Python 3.7 when it heads the shelves later this year.

Why is PyPy so much faster and why doesn’t everyone just use that instead?

PyPy is faster than CPython as it has a Just-in-time compiler. JIT compilers have a great benefit in that they are very efficient at executing predictable, repetitive tasks. One of the natures of benchmarks is that you try to run the same piece of code multiple times to make it accurate, push the application and reduce error margins. As such PyPy shines on these tests.

The downside to a JIT compiler, and in particular PyPy’s, is the high startup cost. Another downside is the lack of compatibility for many C-Extensions. Because “Python” (CPython, the official PSF Python) is written in C, many 3rd party extensions on PyPi take advantage of this. Numpy would be a good example, much of Numpy is written in optimized C code. When you pip install numpy it uses your local C compiler and builds a binary library for your Python runtime to use.

Because PyPy is written in Python, many modules simply cannot work in PyPy. So you should always check.

Also, PyPy suffers the same challenge as CPython — the shift from version 2 of the language to version 3. PyPy3 I’ve found to be unstable until recently and in the benchmarks you can still see odd inconsistencies with PyPy. I’ve also had issues with packages (e.g. PyTest) dropping support for PyPy3 whilst they iron out issues.

Conclusion

Python 3.7 is the fastest of the “official” Python’s and PyPy is the fastest implementation I tested.

I would love to see PyPy3 perform just as well, if not better than PyPy in the future as Python 2 becomes less-used.

Still stuck on Python 2?

Check out my new course on Pluralsight for moving from Python 2 to 3.