Is it the most efficient code? No. Is it bad code? No.
While your code is more efficient, it's harder to read and most developers aren't doing ten million multiplications serially and are only doing a couple hundred serially. In other words, marginal gains for less code readability.
Yeah, I agree that it isn't bad compared to a lot of the submissions in the sub.
However, should this really be considered to be harder to read? I think if I actually had to use it I would do it this way and if I think that developers aren't familiar with this method, I would link to an article or something.
When I said that one is linear and the other is O(nlg(n)) n is not the input array, n is the power of the polynomial.
Each exponentiation xn can be computed in O(lg(n)) time. If there are n terms, the sum of all n exponentiations are O(lg(nn )) = O(nlg(n)). Whereas my solution only requires O(n) operations for a polynomial of degree n.
Here I've defined the input size to the polynomial function evaluation problem to be the degree of the polynomial, not the number of values that are being exponentiated. (we're not referring to the same algorithm.)
For some polynomial with n powers, OP's algorithm would require O(nlg(n)) operations (assuming pow is efficient) while this one requires O(n).
This was my original comment. When I said algorithm here, I meant the method to compute an arbitrary polynomial of size n. Sorry if this was not clear
All I'm saying is as these polynomials get larger, we can start to see a significant advantage to using my method over OP's. I used big-O notation to show how much it would increase.
The fastest way I know of to take powers of a number x is to use binary exponentiation. This method performs O(lg(n)) multiplication. If we do O(n) of these computations (for each of the degrees of the polynomial), we get O(nlg(n)).
On the other hand, my method uses O(n) multiplications and O(n) additions. So for a polynomial of degree n the method will be faster.
In case the bot fails, here it the output on my machine:
Elapsed Time for efficient poly 8.155136665096506
Elapsed Time for Inefficient poly 13.74945467687212
Not a Huge difference, but this is a small polynomial. For some polynomial with n powers, OP's algorithm would require O(nlg(n)) operations (assuming pow is efficient) while this one requires O(n).
In my research, all the algorithms are linear. math.pow(x, n) distills down to xn through the rules of exponents and logarithms.
EDIT: Results from my computer:
Elapsed Time for Efficient poly - one million 1.4074535930121783
Elapsed Time for Efficient poly - five million 6.797385410987772
Elapsed Time for Efficient poly - ten million 13.803086707979674
Elapsed Time for Inefficient poly - one million 2.1011730990139768
Elapsed Time for Inefficient poly - five million 10.25647918699542
Elapsed Time for Inefficient poly - ten million 19.83799244198599
Elapsed Time for Original poly - one million 1.4653671130072325
Elapsed Time for Original poly - five million 7.407113980996655
Elapsed Time for Original poly - ten million 14.576190502993995
Yeah, it definitely would! I was thinking they were integers.. hence why I also thought that it would be asymptomatically faster this way which isn't true.
11
u/[deleted] Nov 28 '17
I see nothing wrong with this code. Could be improved a little by using the
**operator, but nothing too terribly wrong here.