Speed-up lazy heapq import in collections (gh-127538)

This commit is contained in:
Raymond Hettinger 2024-12-02 20:45:36 -06:00 committed by GitHub
parent bfb0788bfc
commit dffb90911a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -59,6 +59,8 @@ try:
except ImportError:
pass
heapq = None # Lazily imported
################################################################################
### OrderedDict
@ -633,7 +635,10 @@ class Counter(dict):
return sorted(self.items(), key=_itemgetter(1), reverse=True)
# Lazy import to speedup Python startup time
import heapq
global heapq
if heapq is None:
import heapq
return heapq.nlargest(n, self.items(), key=_itemgetter(1))
def elements(self):