{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ], "cell_type": "markdown", "metadata": [] }, { "source": [ "# 5.4. Accelerating Python code with Cython" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "We use Cython to accelerate the generation of the Mandelbrot fractal." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "import numpy as np" ], "metadata": {} }, { "source": [ "We initialize the simulation and generate the grid\n", "in the complex plane." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "size = 200\n", "iterations = 100" ], "metadata": {} }, { "source": [ "## Pure Python" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "def mandelbrot_python(m, size, iterations):\n", " for i in range(size):\n", " for j in range(size):\n", " c = -2 + 3./size*j + 1j*(1.5-3./size*i)\n", " z = 0\n", " for n in range(iterations):\n", " if np.abs(z) <= 10:\n", " z = z*z + c\n", " m[i, j] = n\n", " else:\n", " break" ], "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%%timeit -n1 -r1 m = np.zeros((size, size))\n", "mandelbrot_python(m, size, iterations)" ], "metadata": {} }, { "source": [ "## Cython versions" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "We first import Cython." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%load_ext cythonmagic" ], "metadata": {} }, { "source": [ "### Take 1" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "First, we just add the %%cython magic." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%%cython -a\n", "import numpy as np\n", "\n", "def mandelbrot_cython(m, size, iterations):\n", " for i in range(size):\n", " for j in range(size):\n", " c = -2 + 3./size*j + 1j*(1.5-3./size*i)\n", " z = 0\n", " for n in range(iterations):\n", " if np.abs(z) <= 10:\n", " z = z*z + c\n", " m[i, j] = n\n", " else:\n", " break" ], "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%%timeit -n1 -r1 m = np.zeros((size, size), dtype=np.int32)\n", "mandelbrot_cython(m, size, iterations)" ], "metadata": {} }, { "source": [ "Virtually no speedup." ], "cell_type": "markdown", "metadata": {} }, { "source": [ "### Take 2" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "Now, we add type information, using memory views for NumPy arrays." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%%cython -a\n", "import numpy as np\n", "\n", "def mandelbrot_cython(int[:,::1] m, \n", " int size, \n", " int iterations):\n", " cdef int i, j, n\n", " cdef complex z, c\n", " for i in range(size):\n", " for j in range(size):\n", " c = -2 + 3./size*j + 1j*(1.5-3./size*i)\n", " z = 0\n", " for n in range(iterations):\n", " if z.real**2 + z.imag**2 <= 100:\n", " z = z*z + c\n", " m[i, j] = n\n", " else:\n", " break" ], "metadata": {} }, { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%%timeit -n1 -r1 m = np.zeros((size, size), dtype=np.int32)\n", "mandelbrot_cython(m, size, iterations)" ], "metadata": {} }, { "source": [ "Interesting speedup!" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n\n> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ], "cell_type": "markdown", "metadata": {} } ], "metadata": {} } ], "metadata": { "name": "", "signature": "sha256:366b657ed28b4fb92f704f9e62e50cf5d894f1888f3b6082e53265b6e79d9be8" } }