{ "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import numexpr as ne" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We generate three large vectors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x, y, z = np.random.rand(3, 1000000)" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit x + (y**2 + (z*x + 1)*3)" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ne.ncores" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for i in range(1, 5):\n", " ne.set_num_threads(i)\n", " %timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')" ] }, { "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": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }