Skip to content

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

Closed
eendebakpt wants to merge 3 commits into
python:mainfrom
eendebakpt:parse_args_finds
Closed

gh-117431: Improve performance of startswith, endswith, count and find methods for strings and bytes#117432
eendebakpt wants to merge 3 commits into
python:mainfrom
eendebakpt:parse_args_finds

Conversation

@eendebakpt

@eendebakpteendebakpt commented Apr 1, 2024

Copy link
Copy Markdown
Contributor

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
@eendebakpteendebakpt changed the title Draft: gh-117431 Improve performance of startswith, endswith, count and find methods for strings and bytesgh-117431 Improve performance of startswith, endswith, count and find methods for strings and bytesApr 1, 2024
@eendebakpt
eendebakpt marked this pull request as ready for review April 1, 2024 21:16
@AlexWaygoodAlexWaygood added the performance Performance or resource usage label Apr 1, 2024
@erlend-aaslanderlend-aasland changed the title gh-117431 Improve performance of startswith, endswith, count and find methods for strings and bytesgh-117431: Improve performance of startswith, endswith, count and find methods for strings and bytesApr 2, 2024
@eendebakpt

Copy link
Copy Markdown
ContributorAuthor

Closing in favor of #117466

@eendebakpt
eendebakpt deleted the parse_args_finds branch June 26, 2025 13:45
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviewperformancePerformance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@eendebakpt@AlexWaygood