Parallelizing a Numpy vector operation

后端 未结 3 1969
别那么骄傲
别那么骄傲 2020-11-28 02:34

Let\'s use, for example, numpy.sin()

The following code will return the value of the sine for each value of the array a:

im         


        
3条回答
  •  醉话见心
    2020-11-28 02:59

    There is a better way: numexpr

    Slightly reworded from their main page:

    It's a multi-threaded VM written in C that analyzes expressions, rewrites them more efficiently, and compiles them on the fly into code that gets near optimal parallel performance for both memory and cpu bounded operations.

    For example, in my 4 core machine, evaluating a sine is just slightly less than 4 times faster than numpy.

    In [1]: import numpy as np
    In [2]: import numexpr as ne
    In [3]: a = np.arange(1000000)
    In [4]: timeit ne.evaluate('sin(a)')
    100 loops, best of 3: 15.6 ms per loop    
    In [5]: timeit np.sin(a)
    10 loops, best of 3: 54 ms per loop
    

    Documentation, including supported functions here. You'll have to check or give us more information to see if your more complicated function can be evaluated by numexpr.

提交回复
热议问题