abjad
abjad copied to clipboard
Improve performance of membership checks in `replace_at` and `_initialize_color_dictionary` using sets
This PR optimizes membership checks by replacing lists with sets in two key functions:
-
replace_at
function:
- The index_values used in the
if index % index_period in index_values:
check has been converted to aset
for faster membership testing. This change improves performance whenindex_values
contains multiple elements, as thein
operation on aset
has an average time complexity of O(1), compared to O(n) for a list.
-
_initialize_color_dictionary
function:
- The
keys
from the dictionary were previously converted to a list for membership testing. This has been replaced by using aset
, significantly improving performance, especially when the dictionary is large. Thein
operation on theset
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!