45

In python, which one is faster ?

numpy.max(), numpy.min()

or

max(), min()

My list/array length varies from 2 to 600. Which one should I use to save some run time ?

Froyo
  • 17,947
  • 8
  • 45
  • 73
  • 4
    You should definitely test and find out for your specific scenario, but my first instinct is that it would depend on whether your iterables are "vanilla" python iterables or numpy iterables. – jedwards Jun 08 '12 at 04:37
  • 7
    If the data are in a **`list`**, I'd use vanilla `max`. If they are in a numpy **`array`** I'd use `numpy.max`. Converting a list to a numpy array is a pretty expensive operation – John La Rooy Jun 08 '12 at 05:15

4 Answers4

60

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.

gorlum0
  • 1,425
  • 12
  • 12
43

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().

enter image description here

Code to reproduce the plot:

import numpy as np
import perfplot

b = perfplot.bench(
    setup=np.random.rand,
    kernels=[max, np.max, lambda a: a.max()],
    labels=["max(a)", "np.max(a)", "a.max()"],
    n_range=[2 ** k for k in range(25)],
    xlabel="len(a)",
)
b.show()
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
13

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.

srgerg
  • 18,719
  • 4
  • 57
  • 39
  • 2
    I tried that on a random list of 600 floating point numbers. || np.max() + np.min() -> 1.093 msec || max() + min() -> 0.092 msec – Froyo Jun 08 '12 at 04:48
2

numpy.min and numpy.max have slightly different semantics (and call signatures) to the builtins, so the choice shouldn't be to do with speed. Use the numpy versions if you need to be able to handle multidimensional data sanely. If you're just using Python lists or other things that don't know about dimensionality, use the builtins.

lvc
  • 34,233
  • 10
  • 73
  • 98