gh-117431: Improve performance of startswith, endswith, count and find methods for strings and bytes by eendebakpt · Pull Request #117432 · python/cpython · GitHub
main: python -m timeit -s "s = 'abcdef'" "s.startswith('abc')"
5000000 loops, best of 5: 63.8 nsec per loop
pr: python -m timeit -s "s = 'abcdef'" "s.startswith('abc')"
5000000 loops, best of 5: 42.3 nsec per loop
main: python -m timeit -s "s = 'abcdef'" "s.count('a')"
5000000 loops, best of 5: 70 nsec per loop
pr: python -m timeit -s "s = 'abcdef'" "s.count('a')"
5000000 loops, best of 5: 51.4 nsec per loop
main: python -m timeit -s "s = b'abcdef'" "s.startswith(b'q')"
5000000 loops, best of 5: 68.3 nsec per loop
pr: python -m timeit -s "s = b'abcdef'" "s.startswith(b'q')"
5000000 loops, best of 5: 44.5 nsec per loop
Notes:
We could also add a fast path for the case where there are 2 or 3 arguments. Since all arguments are positional only, for 1, 2 or 3 arguments the parsing cannot fail. That will increase performance for the 2 and 3 argument case, but it adds some more complexity.
eendebakpt
changed the title
Draft: gh-117431 Improve performance of startswith, endswith, count and find methods for strings and bytes
gh-117431 Improve performance of startswith, endswith, count and find methods for strings and bytes
Apr 1, 2024
erlend-aasland
changed the title
gh-117431 Improve performance of startswith, endswith, count and find methods for strings and bytes
gh-117431: Improve performance of startswith, endswith, count and find methods for strings and bytes
Apr 2, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmarks (non-PGO):
main: python -m timeit -s "s = 'abcdef'" "s.startswith('abc')" 5000000 loops, best of 5: 63.8 nsec per loop pr: python -m timeit -s "s = 'abcdef'" "s.startswith('abc')" 5000000 loops, best of 5: 42.3 nsec per loopmain: python -m timeit -s "s = 'abcdef'" "s.count('a')" 5000000 loops, best of 5: 70 nsec per loop pr: python -m timeit -s "s = 'abcdef'" "s.count('a')" 5000000 loops, best of 5: 51.4 nsec per loopNotes: