Skip to content

Commit

Permalink
Optimize tween
Browse files Browse the repository at this point in the history
* Move the floating point division out of the inner loop
* Use map() / list comprehensions where possible, in preference to nested loop
* Don't call show() explicitly if _set() already did it
* Regression test (not committed) shows identical _values being written at each step
  • Loading branch information
rowlap committed Nov 3, 2019
1 parent a338e40 commit b8c9956
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions library/piglow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import division

import atexit
import operator
import time
from sys import exit

Expand Down Expand Up @@ -250,16 +253,17 @@ def tween(duration, end, start=None):
if start is None:
start = _values.copy()

deltas = map(operator.sub, end, start)
deltas_per_frame = [d / steps for d in deltas]

new = start
for x in range(steps):
new = []
for y in range(18):
s = start[y]
e = end[y]
c = float(e - s)
b = s + ((c / float(steps)) * (x + 1))
new.append(int(b))
_set(0, new)
show()
new = list(map(operator.add, new, deltas_per_frame))
new_ints = [round(n) for n in new]
_set(0, new_ints)
# avoid double write
if not auto_update:
show()
time.sleep(fps)


Expand Down

0 comments on commit b8c9956

Please sign in to comment.