numpy.max or max ? Which one is faster?
Well from my timings it follows if you already have numpy array a
you should use a.max
(the source tells it's the same as np.max
if a.max
available). But if you have built-in list then most of the time takes converting it into np.ndarray => that's why max
is better in your timings.
In essense: if np.ndarray
then a.max
, if list
and no need for all the machinery of np.ndarray
then standard max
.
I was also interested in this and tested the three variants with perfplot (a little project of mine). Result: You're not going wrong with a.max()
.
Code to reproduce the plot:
import numpyimport perfplotperfplot.show( setup=lambda n: numpy.random.rand(n), kernels=[max, numpy.max, lambda a: a.max()], labels=["max(a)", "numpy.max(a)", "a.max()"], n_range=[2 ** k for k in range(25)], logx=True, logy=True, xlabel="len(a)",)
It's probably best if you use something like the Python timeit module to test it for yourself. That way you can test your own data in your own environment, rather than relying on third parties with various test data and environments which aren't necessarily representative of yours.