My code uses three random numbers and some if statements to move a pixel randomly, using cProfiler, it turns out the function is quite inefficient.
s = self.surrounding()
surroundingBool = [False if i == None else True for i in s]
r1 = random.random()
r2 = random.random()
r3 = random.random()
if r1 <= 0.333:
if r3 < 0.5 and not surroundingBool[4]: self.x += 1
elif not surroundingBool[3]: self.x -= 1
elif r1 <= 0.666:
if r3 < 0.5 and not surroundingBool[6]: self.y += 1
elif not surroundingBool[1]: self.y -= 1
else:
if r2 < 0.25 and not surroundingBool[7]:
self.x += 1
self.y += 1
elif r2 < 0.5 and not surroundingBool[2]:
self.x += 1
self.y -= 1
elif r2 < 0.75 and not surroundingBool[5]:
self.x -= 1
self.y += 1
elif not surroundingBool[0]:
self.x -= 1
self.y -= 1
self.x %= width
self.y %= height
self.pos = self.y * width + self.x
Hopefully this is quite self explanatory but I can provide context as required. How can I make these if else statements faster or more efficient?
The full code can be found here if needed
Thanks.
Aucun commentaire:
Enregistrer un commentaire