This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients rhettinger
Date 2014-05-04.05:57:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1399183029.85.0.254812547035.issue21424@psf.upfronthosting.co.za>
In-reply-to
Content
Consolidate the logic for nlargest() into a single function.  Remove both the C and pure Python base underlying code. 

With all the logic in a single function, it only becomes necessary to create, store, and compare the data tuples when a need item is added to the heap.  This means that the rest of the comparisons (checking to see whether a new item needs to be added to the heap) can run faster and not need to create a (key, order, elem) tuple.

The change reduces the number of tuples created and the number of ordering integers created.

When rich comparisons were introduced, tuple ordering comparisons became twice as expensive (they are compared elementwise for equality and then there is an additional comparison call to order the first differing element).  Under the existing nlargest() code, we pay that price for every lement in the iterable.  In the new code, we pay that price only for the small subset of the iterable that actually gets added to the heap.

After this, another patch for simplifying nsmallest() is forthcoming.
History
Date User Action Args
2014-05-04 05:57:10rhettingersetrecipients: + rhettinger
2014-05-04 05:57:09rhettingersetmessageid: <1399183029.85.0.254812547035.issue21424@psf.upfronthosting.co.za>
2014-05-04 05:57:09rhettingerlinkissue21424 messages
2014-05-04 05:57:09rhettingercreate