NumPy ndarray.all () vs np.all (ndarray) vs all (ndarray)

What is the difference between the three “all” methods in Python / NumPy? What is the reason for the difference in performance? Is it true that ndarray.all () is always the fastest of the three?

Here is the time 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
+4
source share
3 answers

The difference between np.all(a)and a.all()is simple:

  • If it ais numpy.array, then np.all()it will simply call a.all().
  • a numpy.array, np.all() numpy.array, a.all(). a.all() , a numpy.array , , all.

np.all all .

  • all ( list, set s, generators,...). np.all numpy.array ( , numpy, .. list tuple s).
  • np.all array , != 0. all bool , .
  • python , python. np.all .

, a. python, all . , np.all a.all() ( , , object , , ).

+5

+1

I suspect that numpy functions do more to evaluate the element of the array as logical, probably in some generic numerical order, while the built-in all()does nothing, since the elements are already logical.

I wonder how different results will be with integers float.

0
source

Source: https://habr.com/ru/post/1674705/


All Articles