user2015146
user2015146

Reputation:

numpy vs native Python - most efficient way

For a lot of functions, it is possible to use either native Python or numpy to proceed.

This is the case for math functions, that are available with Python native import math, but also with numpy methods. This is also the case when it comes to arrays, with narray from numpy and pythons list comprehensions, or tuples.

I have two questions relative to these features that are in Python and also in numpy

  1. in general, if method is available in native Python AND numpy, which of both solutions would you prefer ? In terms of efficiency ? Is it different and how Python and numpy would differ in their proceeding?

  2. More particularly, regarding arrays, and basic functions that are dealing with arrays, like sort, concatenate..., which solution is more efficient ? What makes the efficiency of the most efficient solution?

This is very open and general question. I guess that will not impact my code greatly, but I am just wondering.

Upvotes: 5

Views: 1916

Answers (3)

VB9-UANIC
VB9-UANIC

Reputation: 330

You can benchark smippets of code and decide on results. Use python module timeit: http://docs.python.org/2/library/timeit.html

Upvotes: 1

NPE
NPE

Reputation: 500963

  1. When there's a choice between working with NumPy array and numeric lists, the former are typically faster.

  2. I don't quite understand the second question, so won't try to address it.

Upvotes: 1

Amber
Amber

Reputation: 527548

In general, it probably matters most (efficiency-wise) to avoid conversions between the two. If you're mostly using non-numpy functions on data, then they'll be internally operating using standard Python data types, and thus using numpy arrays would be inefficient due to the need to convert back and forth.

Similarly, if you're using a lot of numpy functions to manipulate data, transforming it all back to basic Python types in between would also be inefficient.


As far as choosing functions goes, use whichever one was designed to operate on the form your data is already in - e.g. if you already have a numpy array, use the numpy functions on it; similarly, if you have a basic Python data type, use the Python functions on it. numpy's functions are going to be optimized for working with numpy's data types.

Upvotes: 5

Related Questions