-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Polynomial.py Optimization #4610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here's some code to demonstrate the speed advantage (evaluates a polynomial version of exp at pi): NumCalls = 1<<20 #1 million, binary |
I second this. Is there any particular reason to use a bare for-loop? |
there are some complications with subclasses as far as I remember, the test coverage of this function is very poor. Just using reduce like will break stuff, unfortunately I forgot what exactly... |
Note that numpy.polynomial.polynomial.polyval also uses a loop, but seems faster:
Where the first is the numpy/polynomial/polynomial version, the second is numpy.polyval, and the third is reduce. Not sure why numpy.polyval is so slow, but it may not be the loop. |
After some more testing (i.e. increasing the coefficient count to absurd numbers) I'd also say that it's indeed not the loop but instead the initialisation overhead that leads to the differences. Still, having two subtly different functions in the same library is a problem IMHO. |
On Fri, Aug 29, 2014 at 3:58 AM, Benedikt Sauer notifications@github.com
Turns out the difference in speed is Chuck |
On Mon, Sep 1, 2014 at 7:40 PM, Charles R Harris charlesr.harris@gmail.com
Chuck |
On Mon, Sep 1, 2014 at 8:11 PM, Charles R Harris charlesr.harris@gmail.com
Chuck |
I can't reproduce this. What code did you use to show that |
I reversed the order and timed it. It not
|
For completeness
|
Note that python scalars do go the
|
Multiplying a numpy_scalar times a numpy_array is much faster than the other way around. This PR switches the order of multiplication in the polyval function resulting in a speedup of about 5x for scalar values of x. Closes numpy#4610.
polyval in Polynomial.py is about 5 times slower than an implementation based on reduce. I would suggest replacing the following lines (672 and 673):
for i in range(len(p)):
y = x * y + p[i]
return y
with:
return reduce( lambda y, c: y * x + c, p )
The text was updated successfully, but these errors were encountered: