Skip to content

Commit

Permalink
ENH: removing memory allocations from polyval for ~2x speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbierbaum committed May 30, 2016
1 parent e6593fb commit 5d023dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion numpy/lib/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ def polyval(p, x):
x = NX.asarray(x)
y = NX.zeros_like(x)
for i in range(len(p)):
y = y * x + p[i]
# separate out the Horner operation y = y*x + p[i],
# so there are fewer memory allocations
y *= x
y += p[i]
return y

def polyadd(a1, a2):
Expand Down
6 changes: 5 additions & 1 deletion numpy/polynomial/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,11 @@ def polyval(x, c, tensor=True):

c0 = c[-1] + x*0
for i in range(2, len(c) + 1):
c0 = c[-i] + c0*x
# separate out the Horner operation c0 = c[-i] + c0*x,
# so there are fewer memory allocations
c0 *= x
c0 += c[-i]

return c0


Expand Down

0 comments on commit 5d023dc

Please sign in to comment.