views:

196

answers:

2

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

+4  A: 

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.

BatchyX
+6  A: 

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 'import numpy' 'numpy.abs(3.15)'
100000 loops, best of 3: 4.31 usec per loop

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

However, Numpy is fast on arrays:

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

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

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

EOL
would be nice to have the result of python -m timeit -s 'import numpy' -s 'a=numpy.array((1,)*1000)' 'numpy.abs(a)' to show how numpy can be fast with its array
BatchyX
Thank you for the suggestion. Done!
EOL