{ "metadata": { "name": "", "signature": "sha256:9f3072f113fc338fc8166eb9ca37743161b7df7e8b298ed4179c78cf25168c0c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": [], "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.2. Accelerating array computations with Numexpr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's import NumPy and Numexpr." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import numexpr as ne" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We generate three large vectors." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x, y, z = np.random.rand(3, 1000000)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we evaluate the time taken by NumPy to calculate a complex algebraic expression involving our vectors." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit x + (y**2 + (z*x + 1)*3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now, the same calculation performed by Numexpr. We need to give the formula as a string as Numexpr will parse it and compile it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numexpr also makes use of multicore processors. Here, we have 4 physical cores and 8 virtual threads with hyperthreading. We can specify how many cores we want numexpr to use." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ne.ncores" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(1, 5):\n", " ne.set_num_threads(i)\n", " %timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')" ], "language": "python", "metadata": {}, "outputs": [] }, { "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)." ] } ], "metadata": {} } ] }