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

Improve performance of membership checks in replace_at and _initialize_color_dictionary using sets #1590

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

allrob23
Copy link

This PR optimizes membership checks by replacing lists with sets in two key functions:

  1. replace_at function:
  • The index_values used in the if index % index_period in index_values: check has been converted to a set for faster membership testing. This change improves performance when index_values contains multiple elements, as the in operation on a set has an average time complexity of O(1), compared to O(n) for a list.
  1. _initialize_color_dictionary function:
  • The keys from the dictionary were previously converted to a list for membership testing. This has been replaced by using a set, significantly improving performance, especially when the dictionary is large. The in operation on the set reduces the time complexity from O(n) to O(1).

Both changes are aimed at enhancing the performance of membership tests without altering the logic or behavior of the existing code. These improvements are particularly beneficial in cases where the collections being checked contain many elements.

Let me know if you'd like to add or adjust anything!

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.

1 participant