У меня получается так:
result for minmax: (0, 1999999), ‘time spent:7.46938109398
result for minmax1: (0, 1999999), ’time spent:0.92161488533
result for minmax2: (0, 1999999), ‘time spent:0.899652004242
result for minmax3: (0, 1999999), ’time spent:3.92543697357
result for minmax4: (0, 1999999), 'time spent:1.45591902733
Для такого кода:
import functools
import random
from time import time
def minmax1(iterable):
minm = None
maxm = None
for item in iterable:
if maxm is None or item > maxm:
maxm = item
if minm is None or item < minm:
minm = item
return minm, maxm
def minmax( it, key = lambda self: self ):
return functools.reduce( lambda pair, x: (x,x) if pair is None else ( +min(x, pair[0], key=key), max(x, pair[1], key=key) ), it, None )
def minmax2(iterable):
return min(iterable), max(iterable)
def minmax3(iterable):
isorted = sorted(iterable)
return isorted[0], isorted[-1]
def minmax4(it):
def cmp( pair, x ):
if x < pair[0]: return x, pair[1]
if x > pair[1]: return pair[0], x
return pair
return functools.reduce( cmp, it, 2*(it[0],) ) if it else None
data = range(2000000)
random.shuffle(data)
for fun in minmax, minmax1, minmax2, minmax3, minmax4:
start = time()
print("result for {0}: {1}, 'time spent:{2}".format( fun.__name__, fun(data), time() - start ))