Skip to content
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

Improved str.count() algorithm using the Knuth-Morris-Pratt algorithm #2530

Merged
merged 2 commits into from
Feb 17, 2024

Conversation

advikkabra
Copy link
Collaborator

Implemented the Knuth-Morris-Pratt algorithm for str.count(), similar to the str.find() implementation. This should improve speed significantly in many cases.

@advikkabra
Copy link
Collaborator Author

advikkabra commented Feb 12, 2024

As an aside, as recommended by #2355, we can improve these algorithms further and benchmark them if need be.
There may be variations of the Boyer-Moore algorithm which work better, as the issue suggests, but I have used KMP as it is previously used for the string find method. I can work on testing and benchmarking them if the current algorithms are to be changed.

CPython implementation
Blog post explaining more

@advikkabra
Copy link
Collaborator Author

@certik Could you review this PR?

Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@advikkabra thank you for this PR. I think it looks great, thanks for implementing the more advanced algorithm, both at compile time and runtime.

@certik certik merged commit 960fdda into lcompilers:main Feb 17, 2024
13 checks passed
@advikkabra advikkabra deleted the new-str-attributes branch February 18, 2024 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants