Numpy vs native Python is the most efficient way

For a lot of functions, either native Python or numpy can be used to continue.

This applies to math functions that are available with native Python import math

, but also with numpy methods. This is also the case when it comes to arrays, with a description of numpy and pythons list concepts or tuples.

I have two questions regarding these functions which are in Python as well as numpy

  • in general, if a method is available in native Python AND numpy, which of these solutions would you prefer? In terms of efficiency? Is this any different from how Python and numpy will differ in their process?

  • Specifically with regards to arrays and basic functions that deal with arrays like sort, concatenate ... which solution is more efficient? What makes efficiency the most effective solution?

This is a very open and general question. I guess it won't have much effect on my code, but I'm just curious.

+3


source to share


3 answers


Overall, it's probably most important (in terms of performance) to avoid conversions between the two. If you are using non-numpy functions on data most of the time, they will internally work using standard Python data types and therefore using numpy arrays will be inefficient due to the need to convert back and forth.

Likewise, if you use a lot of numpy functions to manipulate data, converting them back to basic Python types in between will also be inefficient.




As for function selection, use what has been designed to work with a form that your data is already in, for example if you already have a numpy array, use numpy functions on it; similarly, if you have a basic Python datatype, use Python functions on it. numpy functions will be optimized to work with numpy data types.

+4


source


  • When there is a choice between working with a NumPy array and numeric lists, the former are usually faster.

  • I don't quite understand the second question, so I am not trying to solve it.



+1


source


You can try out code snippets and decide on the results. Use python time module: http://docs.python.org/2/library/timeit.html

+1


source







All Articles