abjad icon indicating copy to clipboard operation
abjad copied to clipboard

Improve performance of membership checks in `replace_at` and `_initialize_color_dictionary` using sets

Open allrob23 opened this issue 4 months ago • 0 comments

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!

allrob23 avatar Oct 11 '24 18:10 allrob23