This might sound subjective, but it is not. I have an algorithm that runs until it visits all elements in a list, selecting the index randomly on each cycle. The list initially contains N # of booleans equal to False. Once an element is visited, its value is changed to True. If all elements read True, then the algorithm finishes. Assume that you cannot change the underlying algorithm, only the implementation.
Currently it runs too slowly for me to scale it so I'm looking for ways to reduce the runtime. One potential bottleneck I think is the random choice of element. Currently I'm using:
choice = random.choice(list)
Instead of
choice = list[random.randint(0,len(list)-1)]
I don't know the specifics of the underlying implementation of either random.choice() or random.randint(), does anyone know which is more efficient?
Aucun commentaire:
Enregistrer un commentaire