What is the the difference between the three "all" methods in Python/NumPy? What is the reason for the performance difference? Is it true that ndarray.all() is always the fastest of the three?
Here is a timing test that I ran:
In [59]: a = np.full(100000, True, dtype=bool)
In [60]: timeit a.all()
The slowest run took 5.40 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.24 µs per loop
In [61]: timeit all(a)
1000 loops, best of 3: 1.34 ms per loop
In [62]: timeit np.all(a)
The slowest run took 5.54 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 6.41 µs per loop
numpy. array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way.
all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape , which is a tuple of N non-negative integers that specify the sizes of each dimension.
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. On the other hand, a list in Python is a collection of heterogeneous data types stored in non-contiguous memory locations.
The difference between np.all(a)
and a.all()
is simple:
a
is a numpy.array
then np.all()
will simply call a.all()
.a
is not a numpy.array
the np.all()
call will convert it to an numpy.array
and then call a.all()
. a.all()
on the other hand will fail because a
wasn't a numpy.array
and therefore probably has no all
method.The difference between np.all
and all
is more complicated.
all
function works on any iterable (including list
, set
s, generators
, ...). np.all
works only for numpy.array
s (including everything that can be converted to a numpy array, i.e. list
s and tuple
s). np.all
processes an array
with specified data type, that makes it pretty efficient when comparing for != 0
. all
however needs to evaluate bool
for each item, that's much slower.np.all
doesn't need to do that conversion.Note that the timings depend also on the type of your a
. If you process a python list all
can be faster for relativly short lists. If you process an array, np.all
and a.all()
will be faster in almost all cases (except maybe for object
arrays, but I won't go down that path, that way lies madness).
I'll take a swing at this
np.all
is a generic function which will work with different data types, under the hood this probably looks for ndarray.all
which is why it's slightly slower.
all
is a python bulit-in function see https://docs.python.org/2/library/functions.html#all.
ndarray.all
is method of the 'numpy.ndarray' object, calling this directly may be faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With