#!/usr/bin/env python # coding: utf-8 # > 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. # # # 5.2. Accelerating array computations with Numexpr # Let's import NumPy and Numexpr. # In[ ]: import numpy as np import numexpr as ne # We generate three large vectors. # In[ ]: x, y, z = np.random.rand(3, 1000000) # Now, we evaluate the time taken by NumPy to calculate a complex algebraic expression involving our vectors. # In[ ]: get_ipython().run_line_magic('timeit', 'x + (y**2 + (z*x + 1)*3)') # 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. # In[ ]: get_ipython().run_line_magic('timeit', "ne.evaluate('x + (y**2 + (z*x + 1)*3)')") # 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. # In[ ]: ne.ncores # In[ ]: for i in range(1, 5): ne.set_num_threads(i) get_ipython().run_line_magic('timeit', "ne.evaluate('x + (y**2 + (z*x + 1)*3)')") # > You'll find all the explanations, figures, references, and much more in the book (to be released later this summer). # # > [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).