-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144127: Counter.most_common() Optimization for Small n + Test Cases
#144129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2279,6 +2279,24 @@ def test_invariant_for_the_in_operator(self): | |
| self.assertTrue(elem in c) | ||
| self.assertIn(elem, c) | ||
|
|
||
| def test_most_common(self): | ||
| c = Counter(a=5, b=3, c=5, d=2, e=0, f=-1) | ||
|
Comment on lines
+2282
to
+2283
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a particular reason for checking
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, there's not really anything to test against in this PR. If this does increase test coverage, we can accept it in a separate PR, since this one won't be backported. |
||
|
|
||
| self.assertEqual(c.most_common(), [('a', 5), ('c', 5), ('b', 3), ('d', 2), ('e', 0), ('f', -1)]) | ||
| self.assertEqual(c.most_common(3), [('a', 5), ('c', 5), ('b', 3)]) | ||
| self.assertEqual(c.most_common(0), []) | ||
| self.assertEqual(c.most_common(-2), []) | ||
| self.assertEqual(c.most_common(1), [('a', 5)]) | ||
|
|
||
| # Test empty counter | ||
| empty_c = Counter() | ||
|
|
||
| self.assertEqual(empty_c.most_common(), []) | ||
| self.assertEqual(empty_c.most_common(3), []) | ||
| self.assertEqual(empty_c.most_common(0), []) | ||
| self.assertEqual(empty_c.most_common(-2), []) | ||
| self.assertEqual(empty_c.most_common(1), []) | ||
|
|
||
| def test_multiset_operations(self): | ||
| # Verify that adding a zero counter will strip zeros and negatives | ||
| c = Counter(a=10, b=-2, c=0) + Counter() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Optimize :meth:`collections.Counter.most_common` for ``n=1`` to use | ||
| :func:`max` instead of :func:`heapq.nlargest`, improving performance for | ||
| the common case of finding the single most common element. | ||
|
Comment on lines
+1
to
+3
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please note the % increase in performance somewhere here. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the second comment is particularly helpful: