Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()?

like image 866
Mermoz Avatar asked Sep 06 '10 09:09

Mermoz


People also ask

Is NumPy always faster than Python?

There is a big difference between the execution time of arrays and lists. NumPy Arrays are faster than Python Lists because of the following reasons: An array is a collection of homogeneous data-types that are stored in contiguous memory locations.

Is NumPy faster than math?

Numpy is faster because it does the math in C.

Which is better math or NumPy?

The short answer: use math if you are doing simple computations with only with scalars (and no lists or arrays). Use numpy if you are doing scientific computations with matrices, arrays, or large datasets.

What is faster than NumPy?

pandas provides a bunch of C or Cython optimized functions that can be faster than the NumPy equivalent function (e.g. reading text from text files). If you want to do mathematical operations like a dot product, calculating mean, and some more, pandas DataFrames are generally going to be slower than a NumPy array.


1 Answers

Here are the timing results:

lebigot@weinberg ~ % python -m timeit 'abs(3.15)'  10000000 loops, best of 3: 0.146 usec per loop  lebigot@weinberg ~ % 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:

lebigot@weinberg ~ % python -m timeit -s 'a = [3.15]*1000' '[abs(x) for x in a]' 10000 loops, best of 3: 186 usec per loop  lebigot@weinberg ~ % 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!

like image 167
Eric O Lebigot Avatar answered Oct 05 '22 11:10

Eric O Lebigot