summaryrefslogtreecommitdiffstats
path: root/Lib/heapq.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-01-12 10:37:32 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-01-12 10:37:32 (GMT)
commitb5bc33cdabc2361afa60463fedd262a6b457dfde (patch)
tree7eb67aca60b69614fec7bb277ee832ce88ebb6fe /Lib/heapq.py
parentc22ab18e917d483fe95b0288ea096751de71277d (diff)
downloadcpython-b5bc33cdabc2361afa60463fedd262a6b457dfde.zip
cpython-b5bc33cdabc2361afa60463fedd262a6b457dfde.tar.gz
cpython-b5bc33cdabc2361afa60463fedd262a6b457dfde.tar.bz2
Optimize heapq.nsmallest/nlargest for cases where n==1 or n>=size.
Diffstat (limited to 'Lib/heapq.py')
-rw-r--r--Lib/heapq.py47
1 files changed, 46 insertions, 1 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index c13d650..3233cdb 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -129,7 +129,7 @@ From all times, sorting has always been a Great Art! :-)
__all__ = ['heappush', 'heappop', 'heapify', 'heapreplace', 'merge',
'nlargest', 'nsmallest', 'heappushpop']
-from itertools import islice, repeat, count, imap, izip, tee
+from itertools import islice, repeat, count, imap, izip, tee, chain
from operator import itemgetter, neg
import bisect
@@ -354,10 +354,32 @@ def nsmallest(n, iterable, key=None):
Equivalent to: sorted(iterable, key=key)[:n]
"""
+ # Short-cut for n==1 is to use min() when len(iterable)>0
+ if n == 1:
+ it = iter(iterable)
+ head = list(islice(it, 1))
+ if not head:
+ return []
+ if key is None:
+ return [min(chain(head, it))]
+ return [min(chain(head, it), key=key)]
+
+ # When n>=size, it's faster to use sort()
+ try:
+ size = len(iterable)
+ except (TypeError, AttributeError):
+ pass
+ else:
+ if n >= size:
+ return sorted(iterable, key=key)[:n]
+
+ # When key is none, use simpler decoration
if key is None:
it = izip(iterable, count()) # decorate
result = _nsmallest(n, it)
return map(itemgetter(0), result) # undecorate
+
+ # General case, slowest method
in1, in2 = tee(iterable)
it = izip(imap(key, in1), count(), in2) # decorate
result = _nsmallest(n, it)
@@ -369,10 +391,33 @@ def nlargest(n, iterable, key=None):
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
"""
+
+ # Short-cut for n==1 is to use max() when len(iterable)>0
+ if n == 1:
+ it = iter(iterable)
+ head = list(islice(it, 1))
+ if not head:
+ return []
+ if key is None:
+ return [max(chain(head, it))]
+ return [max(chain(head, it), key=key)]
+
+ # When n>=size, it's faster to use sort()
+ try:
+ size = len(iterable)
+ except (TypeError, AttributeError):
+ pass
+ else:
+ if n >= size:
+ return sorted(iterable, key=key, reverse=True)[:n]
+
+ # When key is none, use simpler decoration
if key is None:
it = izip(iterable, imap(neg, count())) # decorate
result = _nlargest(n, it)
return map(itemgetter(0), result) # undecorate
+
+ # General case, slowest method
in1, in2 = tee(iterable)
it = izip(imap(key, in1), imap(neg, count()), in2) # decorate
result = _nlargest(n, it)