Is there an accepted efficient way to find the range (ie. max value - min value) of a list of numbers in python? I have tried using a loop and I know I can use the min
and max
functions with subtraction. I am just wondering if there is some kind of built-in that is faster.
If you really need high performance, try Numpy. The function numpy.ptp
computes the range of values (i.e. max - min
) across an array.
You're unlikely to find anything faster than the min
and max
functions.
You could possibly code up a minmax
function which did a single pass to calculate the two values rather than two passes but you should benchmark this to ensure it's faster. It may not be if it's written in Python itself but a C routine added to Python may do it. Something like (pseudo-code, even though it looks like Python):
def minmax (arr):
if arr is empty:
return (None, None)
themin = arr[0]
themax = arr[0]
for each value in arr[1:]:
if value < themin:
themin = value
else:
if value > themax:
themax = value
return (themin, themax)
Another possibility is to interpose your own class around the array (this may not be possible if you want to work on real arrays directly). This would basically perform the following steps:
themin
and themax
to that value.themin
and themax
depending on how the new value compares to them.themin
or themax
, mark the array dirty.themin
and themax
.themin
and themax
using loop in above pseudo-code, then set array to be clean.What this does is to cache the minimum and maximum values so that, at worst, you only need to do the big calculation infrequently (after deletion of an element which was either the minimum or maximum). All other requests use cached information.
In addition, the adding of elements keep themin
and themax
up to date without a big calculation.
And, possibly even better, you could maintain a dirty flag for each of themin
and themax
, so that dirtying one would still allow you to use the cached value of the nother.
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