March 2010

Brussels

Natively-compiled Python with Shed Skin

I watched an interesting Google tech talk about Google Native Client and tried it out. Apparently it needs a tool called gclient from the Google Chromium depot_tools. I installed it (using Yaourt in Arch Linux, of course) and was suprised to see some Python files compiled by GCC.

This was done by another Google project called Shed Skin. It's a Python-to-C++ compiler that uses type analysis. I wondered how the performance impact was and wrote a small script that sorts large lists of numbers. The time measurements are shown below:

Setting

  • AMD64 3000+
  • Linux 2.6.32-ARCH x86_64
  • Python 2.6.4
  • Shed Skin 0.3-2
  • Built-in sorting algorithm

Code

#!/usr/bin/python
# -<em>- coding: utf-8 -</em>-
 
from random import randint
from time import time
 
data = []
for i in range(1 << 16):
 data.append(randint(0, 255))
 
start = time()
data.sort()
print time() - start

Running

Using python it's quite obvious:

$ python sort.py

0.0448120727539

or you can just chmod +x sort.py and then enter ./sort.py.

Shed Skin is really simple to use as well:

$ shedskin sort.py

[iterative type analysis..]
[generating c++ code..]

$ make

g++ -O2 -pipe -Wno-deprecated  -I. ...

$ ./sort

0.00659690628052

Results

In the end, Shed Skin was about eight times faster than CPython, which is great, but not as great as I had expected. I wonder how PyPy compares to this.

Implementation1^16 Integers2^23 Integers
Python45ms8350ms
Shed Skin7ms1316ms
Execution time (lower is better)