{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Numexpr" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Numexpr** is a fast numerical expression evaluator for NumPy. \n", "\n", "With it, expressions that operate on arrays (like `3*a+4*b`) are accelerated and use less memory than doing the same calculation in Python.\n", "\n", "In addition, its **multi-threaded capabilities** can make use of all your cores, which may accelerate computations, most specially if they are not memory-bounded.\n", "\n", "Last but not least, `numexpr` can make use of Intel's VML (Vector Math Library, normally integrated in its Math Kernel Library, or MKL). This allows further acceleration of transcendent (i.e., non polynomial) expressions.\n", "\n", "**GitHub**: [https://github.com/pydata/numexpr#what-it-is-numexpr]()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Some Examples \n", "\n", "(gathered from `numexpr` documentation)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "import numpy as np\n", "import numexpr as ne" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "a = np.arange(1e6) # Choose large arrays for better speedups\n", "b = np.arange(1e6)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, ...,\n", " 9.99998000e+05, 9.99999000e+05, 1.00000000e+06])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ne.evaluate(\"a + 1\") # a simple expression" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, ..., True, True, True], dtype=bool)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ne.evaluate('a*b-4.1*a > 2.5*b') # a more complex one" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ nan, 1.72284457, 1.79067101, ..., 1.09567006,\n", " 0.17523598, -0.09597844])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ne.evaluate(\"sin(a) + arcsinh(a/b)\") # you can also use functions" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Time Comparison with Numpy" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 loops, best of 3: 3.11 ms per loop\n" ] } ], "source": [ "%timeit a+1" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 loops, best of 3: 2.8 ms per loop\n" ] } ], "source": [ "%timeit ne.evaluate(\"a + 1\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 loops, best of 3: 15.9 ms per loop\n" ] } ], "source": [ "%timeit a*b-4.1*a > 2.5*b" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 loops, best of 3: 3.13 ms per loop\n" ] } ], "source": [ "%timeit ne.evaluate('a*b-4.1*a > 2.5*b')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### (some) preliminary conclusions\n", "\n", "* numexpr is (generally) slow with small arrays\n", "* numexpr is very fast with large arrays and complex operations\n", "* numpy is terrific with in-place operations" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "# NumExpr supported Datatypes\n", "\n", "* 8-bit boolean (bool)\n", "* 32-bit signed integer (int or int32)\n", "* 64-bit signed integer (long or int64)\n", "* 32-bit single-precision floating point number (float or float32)\n", "* 64-bit, double-precision floating point number (double or float64)\n", "* 2x64-bit, double-precision complex number (complex or complex128)\n", "* Raw string of bytes (str)" ] } ], "metadata": { "celltoolbar": "Slideshow", "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.3" } }, "nbformat": 4, "nbformat_minor": 0 }