Skip to content

Commit 5ce6da5

Browse files
authored
Avoid uncecessary iteration in SequenceLearner (#380)
This change is relevant when a SequenceLearner is used with an object that provides the Sequence interface, but lazily evaluates its elements.
1 parent e06b34c commit 5ce6da5

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

adaptive/learner/sequence_learner.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ class SequenceLearner(BaseLearner):
8181
def __init__(self, function, sequence):
8282
self._original_function = function
8383
self.function = _IgnoreFirstArgument(function)
84-
self._to_do_indices = SortedSet({i for i, _ in enumerate(sequence)})
84+
# prefer range(len(...)) over enumerate to avoid slowdowns
85+
# when passing lazy sequences
86+
self._to_do_indices = SortedSet(range(len(sequence)))
8587
self._ntotal = len(sequence)
8688
self.sequence = copy(sequence)
8789
self.data = SortedDict()

0 commit comments

Comments
 (0)