You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2017-09-08.06:25:50.613>created_at=<Date2016-05-02.04:29:13.320>labels= ['3.7', 'library', 'performance']
title='Difflib quick_ratio() could use Counter()'updated_at=<Date2017-09-08.06:25:50.602>user='https://github.com/mscuthbert'
The implementation used in difflib.SequenceMatcher().quick_ratio() counts how often each member of the sequence (character, list entry, etc.) appears in order to calculate its lower bound.
Counting how often an entry appears in an iterable has been sped up in cPython w/ the _count_elements() c function in _collections.
This patch uses the collections uses collections.Counter() as a way of getting this speedup (and any future speedups in cPython or other implementations); code is somewhat simplified also.
Performance:
There is a slight overhead to creating two collections.Counter() objects rather than simple dicts. On two Mac systems (Python 3.5 on stock Macbook Air, and Py 3.6a0 latest on recent Mac Pro) the new implementation passes the speed of the previous when the length of the iterable is around 60 items. As the number of items increases, the performance gains increase significantly. By 400 items, the new implementation's speed is about 3x the old, and seems to approach 3.6x asymptotically.
Below 60 items, the older implementation is faster; reaching a max of 8x the speed of the new when comparing a string of one element against a string of one element. (The degenerate case of comparing one or two empty iterables is checked for in this implementation and is thus faster than the old implementation). I believe that users using quick_ratio() are more likely to be looking for speed improvements on larger
Like the previous implementation, the count of the items in seq2 (self.b) is cached; if run again, it is about 41% faster (compared to 47% faster before).
This is my first patch submission, so any suggestions would be appreciated.
@wolma -- you're right, that the inplace __iand__ version of Counter is substantially faster -- it is still slower than the current code (since it is still basically a superset of it). However, testing shows that it is close enough to the current code as to potentially be worth using it, in order to avoid the same issue that arose here (a small speed tweak in this code prevents it from taking advantage of larger improvements later).
If you think that is worth changing, I can make a new patch that incorporates it. I don't have formal benchmarks on it, but quick tests show a 10% regression from the current speedups to using &= instead of the custom summation method.
mscuthbert commented on May 3, 2016
mscuthbertmannequin
MannequinAuthor
More actions
Here are the results I obtained along with the test code I used to get the results. The test code also has a "hybrid" code which I did not propose, but maybe I should have, which uses the old code for very short (but not degenerate) tests and then the new code for larger tests. It does add code complexity however.
I've tried to get the system to not be slower on small sets by not creating a Counter for less than 60 items, and managed to get within 10% of the speed for small sequences while maintaining the 3-3.6x speedup for big comparisons and testing that the results were unchanged, but the code just became far too unmanageable for such a small function as quick_ratio, with code forking that would be hard to maintain.
I'll upload the stub of the replacement for quick_ratio for future developments, in case the creation of Counter objects (the bottleneck) ever disappears, in which case this code will be simpler and faster than the current implementation, but until then, I think it's best to close this issue.
Activity
mscuthbert commentedon May 2, 2016
The implementation used in difflib.SequenceMatcher().quick_ratio() counts how often each member of the sequence (character, list entry, etc.) appears in order to calculate its lower bound.
Counting how often an entry appears in an iterable has been sped up in cPython w/ the _count_elements() c function in _collections.
This patch uses the collections uses collections.Counter() as a way of getting this speedup (and any future speedups in cPython or other implementations); code is somewhat simplified also.
Performance:
There is a slight overhead to creating two collections.Counter() objects rather than simple dicts. On two Mac systems (Python 3.5 on stock Macbook Air, and Py 3.6a0 latest on recent Mac Pro) the new implementation passes the speed of the previous when the length of the iterable is around 60 items. As the number of items increases, the performance gains increase significantly. By 400 items, the new implementation's speed is about 3x the old, and seems to approach 3.6x asymptotically.
Below 60 items, the older implementation is faster; reaching a max of 8x the speed of the new when comparing a string of one element against a string of one element. (The degenerate case of comparing one or two empty iterables is checked for in this implementation and is thus faster than the old implementation). I believe that users using quick_ratio() are more likely to be looking for speed improvements on larger
Like the previous implementation, the count of the items in seq2 (self.b) is cached; if run again, it is about 41% faster (compared to 47% faster before).
This is my first patch submission, so any suggestions would be appreciated.
serhiy-storchaka commentedon May 2, 2016
Could you provide a script or a data used by you for benchmarking, so we can repeat this?
wm75 commentedon May 2, 2016
Given your comment about sum((fullacount & fullbcount).values()), why not use its in-place version:
fullacount &= fullbcount
matches = sum(fullacount.values())
?
mscuthbert commentedon May 3, 2016
@wolma -- you're right, that the inplace __iand__ version of Counter is substantially faster -- it is still slower than the current code (since it is still basically a superset of it). However, testing shows that it is close enough to the current code as to potentially be worth using it, in order to avoid the same issue that arose here (a small speed tweak in this code prevents it from taking advantage of larger improvements later).
If you think that is worth changing, I can make a new patch that incorporates it. I don't have formal benchmarks on it, but quick tests show a 10% regression from the current speedups to using &= instead of the custom summation method.
mscuthbert commentedon May 3, 2016
Here are the results I obtained along with the test code I used to get the results. The test code also has a "hybrid" code which I did not propose, but maybe I should have, which uses the old code for very short (but not degenerate) tests and then the new code for larger tests. It does add code complexity however.
Results:
mscuthbert commentedon May 16, 2017
Poking to see if there's still interest in getting this into 3.7. Thanks!
mscuthbert commentedon Sep 8, 2017
I've tried to get the system to not be slower on small sets by not creating a Counter for less than 60 items, and managed to get within 10% of the speed for small sequences while maintaining the 3-3.6x speedup for big comparisons and testing that the results were unchanged, but the code just became far too unmanageable for such a small function as quick_ratio, with code forking that would be hard to maintain.
I'll upload the stub of the replacement for quick_ratio for future developments, in case the creation of Counter objects (the bottleneck) ever disappears, in which case this code will be simpler and faster than the current implementation, but until then, I think it's best to close this issue.