I was looking to optimize a simple given code that generates a random number ([0,1,2]) that is not in a given list. The random number generator is TRandom3 from ROOT.
def getNumber(noList, randomgen):
#Fügen Sie hier Ihren Code ein!: 💩
i = randomgen.Integer(3)
while i in noList:
i = randomgen.Integer(3)
return i
It is very basic and just generates new numbers until an allowed one is reached.
My own optimized code looked like this:
def bessereAuswahl(noList):
return random.choice([elem for elem in [0,1,2] if elem not in noList])
I just remove all not allowed numbers from my list [0,1,2] and use random.choice to pick one element.
Running on windows 10 I had a performance increase, running the same code on linux I had a performance decrease.
Why is that the case?
Is there a hidden performance penalty for random on linux or is it a performance boost in pyroot?
Aucun commentaire:
Enregistrer un commentaire