make Mnemonic.mnemonic_decode faster

list.index(word) is O(n)
dict[word] is O(log(n))

This makes a difference for Mnemonic.make_seed which
calls self.mnemonic_decode repeatedly.
This commit is contained in:
Manuel Barkhau 2019-11-14 18:54:14 +00:00
parent c2c291dd3a
commit b3f913340c

View file

@ -126,6 +126,7 @@ class Mnemonic(Logger):
self.logger.info(f'language {lang}')
filename = filenames.get(lang[0:2], 'english.txt')
self.wordlist = load_wordlist(filename)
self.wordlist_indexes = {w: i for i, w in enumerate(self.wordlist)}
self.logger.info(f"wordlist has {len(self.wordlist)} words")
@classmethod
@ -156,7 +157,7 @@ class Mnemonic(Logger):
i = 0
while words:
w = words.pop()
k = self.wordlist.index(w)
k = self.wordlist_indexes[w]
i = i*n + k
return i