[issue4790] Optimization to heapq module

Raymond Hettinger report at bugs.python.org
Wed Dec 31 02:49:03 CET 2008


Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:

Am rejecting the patch because it violates the sort equivalence
guarantee (including sort's promise to maintain stability).

If you need the speed and don't care about sort stability, then just use
_heapq.nsmallest() or _heapq.nlargest() directly.

We could complexify the code a bit to achieve some automatic speed-up in
the case of key==None, but my timings don't show much of an improvement:

    if key is None:
        it = izip(iterable, count())                 # decorate
        result = _nsmallest(n, it)
        return map(itemgetter(0), result)            # undecorate
    else:
        in1, in2 = tee(iterable)
        it = izip(imap(key, in1), count(), in2)      # decorate
        result = _nsmallest(n, it)
        return map(itemgetter(2), result)            # undecorate

----------
assignee:  -> rhettinger
nosy: +rhettinger
resolution:  -> rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4790>
_______________________________________


More information about the Python-bugs-list mailing list