High Performance Python (from Training at EuroPython 2011) by Ian Ozsvald - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

CHAPTER

TWENTY THREE

 

OTHER EXAMPLES?

In my examples I’ve used numpy to convert the output array into an RGB string for PIL. Since numpy isn’t supported by PyPy this code won’t work there.  John Montgomery (http://www.littlespikeyland.com/ thanks!)  has submitted a patch which replaces numpy with the array module, modify your code accordingly if you’d like to run it in PyPy:

try:

import array

output = ((o + (256*o) + (256**2)*o) * 8 for o in output)

output = array.array(’I’, output)

#import numpy as np

#output = np.array(output)

#output = (output + (256*output) + (256**2)*output) * 8

 

import Image

im = Image.new("RGB", (w/2, h/2))

im.fromstring(output.tostring(), "raw", "RGBX", 0, -1)

im.show()

except ImportError as err:

# Bail gracefully if we’re using PyPy

print "Couldn’t import Image or numpy:", str(err)

 

During the tutorial I mentioned the refactoring tool http://rope.sourceforge.net/ - the GUI is somewhat primitive (I’ve not tried hooking it into other editors yet) but the refactorings work on large files (e.g. 5,000 lines of Python). I’ve used it to refactor unwieldy client code, pulling out functions, timing them, then improving their speed. I’d suggest you check it out.

For this report I’d be interested in seeing the following examples implemented using the same code format as above (I’ve listed them as most-to-least interesting). I’ve not made these myself as I haven’t tried any of them yet. If you want to put an example together, please send it through to me:

  • Copperhead
  • Theano
  • pure C implementation (this must produce exactly the same validation sum) for reference
  • pyOpenCL
  • execnet (parallel execution environment that distributes binary libraries and allows use of different Python VMs)
  • pyMPI (which opens the door to more parallelisation in scientific environments)
  • Celery (which opens the door to more parallelisation in web-dev environments)
  • Hadoop and Map/Reduce with Python bindings
  • ctypes using C implementation so Python is the nice wrapper
  • Final versions of ShedSkin and Cython examples which go “as fast as possible”
  • Additional compiler flags that would make ShedSkin and Cython go faster (without changing correctness)

23.1 Thanks

I’d like to express my thanks again to the EuroPython 2011 organisers, I had an awful lot of fun preparing and giving this tutorial!

You may also like...