Confusing performance results for prime
Bengt Richter
bokr at oz.net
Sun Oct 19 13:06:40 EDT 2003
On Sat, 18 Oct 2003 17:20:34 -0500, "Greg Brunet" <gregbrunet at NOSPAMsempersoft.com> wrote:
[...]
>
>Bengt:
>
>I can understand that changing from range to xrange might help things
>slightly, and there are other areas as for improvement as well. It's
>the 2 areas that I mentioned that resulted in some unexpected results
>for me that I still didn't understand. I've taken your improvements on
>running & comparing multiple tests (neat stuff BTW) and found the
>following:
>
> - Changing from range to xrange doesn't make an appreciable
>difference (it flip-flops back & forth over multiple runs). I believe I
>had noticed this before, I've now got that actually built into the test
>now.
I guess there must be some optimization at work.
>
> - Getting rid of the Sqrt requires you to perform the (x*x>y) type
>test into the loop (whereas performing the sqrt operation allows you to
>move that calculation outside of the loop and do a simpler
>(factor>limit) test inside the loop). Even though I realized that this
Yes, that was a blunder on my part.
>means I need to import math (at least the sqrt function), I figured that
>this still be better (which I think is Georgy's point). As I increase
>the upperlimit, the sqrt version gets better & better compared to the
>x*x version (as one would expect - trading off a more 'expensive'
>operation in order to move it outside of the loop). I did tests at
>100k, 200k, & 300k, and by 300k, the sqrt test was about 20% faster than
>the x*x test.
>
>I've noted some other things that I've tried and put an updated test
>program in a response to my original message. Thanks for your help -
>it's helped me understand where the improvements were coming from and
>that's what my main goal was all about.
>
Lulu is right about micro-optimization though ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list