Skip to content

Commit 182dde2

Browse files
committed
optimize collection_index
1 parent 8250483 commit 182dde2

File tree

1 file changed

+8
-2
lines changed
  • hypothesis-python/src/hypothesis/internal/conjecture

1 file changed

+8
-2
lines changed

hypothesis-python/src/hypothesis/internal/conjecture/choice.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ def collection_index(choice, *, min_size, alphabet_size, to_order=identity):
6969
# We then add each element c to the index, starting from the end (so "ab" is
7070
# simpler than "ba"). Each loop takes c at position i in the sequence and
7171
# computes the number of sequences of size i which come before it in the ordering.
72-
for i, c in enumerate(reversed(choice)):
73-
index += (alphabet_size**i) * to_order(c)
72+
73+
# this running_exp computation is equivalent to doing
74+
# index += (alphabet_size**i) * n
75+
# but reuses intermediate exponentiation steps for efficiency.
76+
running_exp = 1
77+
for c in reversed(choice):
78+
index += running_exp * to_order(c)
79+
running_exp *= alphabet_size
7480
return index
7581

7682

0 commit comments

Comments
 (0)