Skip to content

gh-117431: Improve performance of startswith, endswith, count and find methods for strings and bytes #117432

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

Closed
wants to merge 3 commits into from

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Apr 1, 2024

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 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 eendebakpt marked this pull request as draft April 1, 2024 20:19
@eendebakpt 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
@eendebakpt eendebakpt marked this pull request as ready for review April 1, 2024 21:16
@AlexWaygood AlexWaygood added the performance Performance or resource usage label Apr 1, 2024
@AlexWaygood AlexWaygood requested a review from gvanrossum April 2, 2024 14:38
@erlend-aasland 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
@eendebakpt
Copy link
Contributor Author

Closing in favor of #117466

@eendebakpt eendebakpt closed this Apr 2, 2024
@eendebakpt eendebakpt deleted the parse_args_finds branch June 26, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants