Optimize comet draw

The big pay-off is avoiding enumerate(). Removing redundant
comparisons of _ring() and avoiding modulo operations help too.
This commit is contained in:
Jeff Epler 2022-11-10 15:50:26 -06:00
parent 914bb8060b
commit 73d5f61c6e
No known key found for this signature in database
GPG key ID: D5BF15AB975AB4DE

View file

@ -120,14 +120,24 @@ class Comet(Animation):
colors = self._comet_colors
if self.reverse:
colors = reversed(colors)
for pixel_no, color in enumerate(colors):
draw_at = self._tail_start + pixel_no
if draw_at < 0 or draw_at >= self._num_pixels:
if not self._ring:
continue
draw_at = draw_at % self._num_pixels
self.pixel_object[draw_at] = color
pixels = self.pixel_object
start = self._tail_start
npixels = len(pixels)
if self._ring:
start %= npixels
for color in colors:
pixels[start] = color
start += 1
if start == npixels:
start = 0
else:
for color in colors:
if start >= npixels:
break
if start >= 0:
pixels[start] = color
start += 1
self._tail_start += self._direction