Python JIT Benchmarks: Compiled Python 3

This page tests the PyPy JIT-compiler on Python 3 programs. It benchmarks PyPy and Python.exe.
JIT Tests. An advanced compiler makes a language faster. The PyPy project implements JIT compilation for Python 3.0 programs. Existing scripts need no modification. PyPy replaces Python.exe. And programs gain speed.
Start. It is easy to set up PyPy on a Windows computer. Visit the PyPy website and download the Windows binary. And then use PyPy.exe instead of Python.exe in your command lines. This resulted in no errors for my programs.PyPy:

And: Performance improved in my tests. With PyPy, most benchmarks executed twice as fast as with Python.exe.

Versions used: ... PyPy 3.2.1 beta 1 ... Python 3.2.4
Example. This example calls index on a list. It searches for the value 40 within the list. Both Python.exe and PyPy.exe return the correct index of 10. But PyPy finishes in about half the time. The JIT-compiler helps with this test.

Note: This benchmark does not only test the index method. It also tests the while-loop. It tests the entire benchmark code.

Update: A final version of PyPy, released in June 2014, is over 40 times faster than the default Python implementation.

Python program that times list.index import time # Input list. list = [1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 40, 400] print(list.index(40)) # Time 1 print(time.time()) # Search list. i = 0 while i < 10000000: result = list.index(40) i += 1 # Time 2 print(time.time()) Output, python.exe 3.3 10 1380996881.864 1380996889.522 [Python = 7.66 s] Output, pypy.exe 2.1 beta 10 1380996852.166 1380996856.023 [PyPy = 3.86 s] Output, pypy.exe 2.3.1 10 1403310001.637 1403310001.822 [PyPy = 0.18 s]
Loops. I wanted to determine if the JIT-compiler has greater advantages on pure loops. This program does nothing but loops. It has a nested while-loop that adds all numbers between 0 and 999. PyPy here delivers about the same performance boost.

So: The JIT-compiler offers improvements for methods (such as index on list) but also for pure loop programs.

Update: The first final version of PyPy3, version 2.3.1, runs this benchmark over 40 times faster than python.exe.

Python program that times loops import time # Time 1 print(time.time()) # Sum numbers. i = 0 while i < 10000: a = 0 sum = 0 while a < 1000: sum += a a += 1 i += 1 # Time 2 print(time.time()) Output, python.exe 3.3 1381010189.814 1381010193.179 [Python = 3.37 s] Output, pypy.exe 2.1 beta 1381010209.825 1381010211.672 [PyPy = 1.85 s] Output, pypy.exe 2.3.1 1403310289.35 1403310289.429 [PyPy = 0.08 s]
Languages. How does PyPy compare to a compiled language like C#? I wrote two programs that sum 1,000 numbers 10,000 times. The C# program completed the task over 20 times faster than the Python 3 one compiled with PyPy3 2.3.1.

So: For loop-intensive, computational programs, Python (with no numeric extensions used) will execute slower.

But: I feel this result is impressive for Python. Its programs are hard to compile because they lack explicit types.

C# program that sums numbers using System; using System.Diagnostics; class Program { static void Main() { var s1 = Stopwatch.StartNew(); for (int i = 0; i < 10000; i++) { int total = 0; for (int a = 0; a < 1000; a++) { total += a; } } s1.Stop(); Console.WriteLine(s1.ElapsedMilliseconds); Console.Read(); } } Output .NET total = 3 ms Python program that sums numbers import time print(time.time()) i = 0 while i < 10000: v = 0 total = 0 while v < 1000: total += v v += 1 i += 1 print(time.time()) Output 1410106718.322 1410106718.402, PyPy total = 80 ms
Update. In June 2014 the first non-beta version of PyPy for the Python 3 language was released. In my initial tests, I found it to be far superior than previous versions. It runs my benchmarks many times faster.

Tip: The PyPy project accepts donations. If you like extremely fast Python 3 programs and have extra money, consider it.

Discussion. Not all programs need an optimizing compiler. But when choosing a language, the safest choice is a language that can be compiled. Down the road, this will alleviate any potential performance problems.

And: With a Python compiler, we can develop complex applications in the Python language with fewer performance concerns.

Thus: The PyPy project makes Python 3 programs a safer choice for large, potentially complex applications.

Summary. Python has been traditionally a slow language. Interpreted languages, or scripting languages, have been at a performance disadvantage. With newer compilers, though, the difference between C-like languages and "scripting" languages narrows.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls