def apply_polynomial( deriv, x ): sum = deriv[0] div=1 xpow=1 for i from 1 to length( deriv ): div*=i xpow*=x sum += deriv[i] * xpow / div return sum
Its about as fast as the second, nearly as readable as the first, and works on any poly. (except the zero poly symbolized by the empty list. )
The bit about the tradeoffs is correct as far as I can tell.
Although if a single solution was by far the best under every metric, there wouldn’t be any tradeoffs.
In most real cases, the solution space is large, and there are many metrics. This means that its unusual for one solution to be the best by all of them. And in those situations, you might not see a choice at all.
Although if a single solution was by far the best under every metric, there wouldn’t be any tradeoffs.
Yes, I debated mentioning this in the post. If a single solution was the best under any metric, then that solution would quickly fade into the background, I think. When I get dressed in the morning, I don’t celebrate the fact that putting on socks before shoes is both easier and more comfortable than the reverse!
A more sensible way to code this would be [...]
I haven’t tested it, but that involves extra multiplication for computing x1 as 1∗x and for multiplying numbers together to get the factorial values. But I haven’t tested it! Maybe I’ll try that today and see if it really is as fast. (The function gets called ~1.7 million times, so even a small difference will be worth keeping the faster code.)
Whoa! I wasn’t expecting so much of a difference. Did you use ```for i in range(...)``` for your loop? That range() call returns an iterator, which I imagine isn’t too fast.
A more sensible way to code this would be
def apply_polynomial( deriv, x ):
sum = deriv[0]
div=1
xpow=1
for i from 1 to length( deriv ):
div*=i
xpow*=x
sum += deriv[i] * xpow / div
return sum
Its about as fast as the second, nearly as readable as the first, and works on any poly. (except the zero poly symbolized by the empty list. )
The bit about the tradeoffs is correct as far as I can tell.
Although if a single solution was by far the best under every metric, there wouldn’t be any tradeoffs.
In most real cases, the solution space is large, and there are many metrics. This means that its unusual for one solution to be the best by all of them. And in those situations, you might not see a choice at all.
Yes, I debated mentioning this in the post. If a single solution was the best under any metric, then that solution would quickly fade into the background, I think. When I get dressed in the morning, I don’t celebrate the fact that putting on socks before shoes is both easier and more comfortable than the reverse!
I haven’t tested it, but that involves extra multiplication for computing x1 as 1∗x and for multiplying numbers together to get the factorial values. But I haven’t tested it! Maybe I’ll try that today and see if it really is as fast. (The function gets called ~1.7 million times, so even a small difference will be worth keeping the faster code.)
Your right. I did some python. My version took 1.26, yours 0.78 microseconds. My code is just another point on the Pareto boundary.
Whoa! I wasn’t expecting so much of a difference. Did you use ```for i in range(...)``` for your loop? That range() call returns an iterator, which I imagine isn’t too fast.