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.
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.
source to share
You can try out code snippets and decide on the results. Use python time module: http://docs.python.org/2/library/timeit.html
source to share