Are NumPy’s math functions faster than Python’s?

I have a function defined by a combination of basic math functions (abs, cosh, sinh, exp, …).

I was wondering if it makes a difference (in speed) to use, for example,
numpy.abs() instead of abs()?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here are the timing results:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3bfb6b1bab4bca793a4b6babdb1b6a1b4">[email protected]</a> ~ % python -m timeit 'abs(3.15)' 
10000000 loops, best of 3: 0.146 usec per loop

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5c9c0c7ccc2cad1e5d2c0cccbc7c0d7c2">[email protected]</a> ~ % python -m timeit -s 'from numpy import abs as nabs' 'nabs(3.15)'
100000 loops, best of 3: 3.92 usec per loop

numpy.abs() is slower than abs() because it also handles Numpy arrays: it contains additional code that provides this flexibility.

However, Numpy is fast on arrays:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cf0f9fef5fbf3e8dcebf9f5f2fef9eefb">[email protected]</a> ~ % python -m timeit -s 'a = [3.15]*1000' '[abs(x) for x in a]'
10000 loops, best of 3: 186 usec per loop

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="573b32353e3038231720323e3935322530">[email protected]</a> ~ % python -m timeit -s 'import numpy; a = numpy.empty(1000); a.fill(3.15)' 'numpy.abs(a)'
100000 loops, best of 3: 6.47 usec per loop

(PS: '[abs(x) for x in a]' is slower in Python 2.7 than the better map(abs, a), which is about 30 % faster—which is still much slower than NumPy.)

Thus, numpy.abs() does not take much more time for 1000 elements than for 1 single float!

Method 2

You should use numpy function to deal with numpy’s types and use regular python function to deal with regular python types.

Worst performance usually occurs when mixing python builtins with numpy, because of types conversion. Those type conversion have been optimized lately, but it’s still often better to not use them. Of course, your mileage may vary, so use profiling tools to figure out.

Also consider the use of programs like cython or making a C module if you want to optimize further your program. Or consider not to use python when performances matters.

but, when your data has been put into a numpy array, then numpy can be really fast at computing bunch of data.

Method 3

In fact, on numpy array

built in abs calls numpy’s implementation via __abs__, see Why built-in functions like abs works on numpy array?

So, in theory there shouldn’t be much performance difference.

import timeit

x = np.random.standard_normal(10000)

def pure_abs():
    return abs(x)

def numpy_abs():
    return np.absolute(x)

n = 10000

t1 = timeit.timeit(pure_abs, number = n)
print 'Pure Python abs:', t1
t2 = timeit.timeit(numpy_abs, number = n)
print 'Numpy abs:', t2
Pure Python abs: 0.435754060745
Numpy abs: 0.426516056061


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x