Generators/iterators, Pythonicity, and primes

Aaron Brady castironpi at gmail.com
Mon Apr 13 07:52:40 EDT 2009


On Apr 13, 1:39 am, Arnaud Delobelle <arno... at googlemail.com> wrote:
> Duncan Booth <duncan.bo... at invalid.invalid> writes:
> > Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>
> >> John Posner <jjpos... at snet.net> wrote:
>
> >>> Do know what in the itertools implementation causes adding a 'if p <=
> >>> sqrt(n)' clause to *decrease* performance, while adding a
> >>> 'takewhile()' clause *increases* performance?
>
> >> I haven't timed it, but I would guess that the takewhile was faster
> >> only because the sqrt(n) had been factored out of the loop. Try the
> >> original loop again precalculating the sqrt(n) and see how that compares.
>
> > Which of course is rubbish, extracting the sdqrt will have an effect but
> > the main factor is that takewhile exits the loop as soon as the condition
> > is false whereas a conditional in a generator comprehension doesn't stop
> > the loop continuing to the end.
>
> Absolutely!  Since you hadn't quoted the original code, I'd forgotten
> that it wasn't stopping after n**0.5.
>
> --
> Arnaud

-1 entertaining, list.  Come on, people, the show must go on.  Take
five.



More information about the Python-list mailing list