Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Duncan Booth
Duncan Booth wrote: > John Posner 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 fa

Re: Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread John Posner
Duncan Booth wrote: John Posner 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 on

Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Duncan Booth
John Posner 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) ha

Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-11 Thread John Posner
Arnaud Delobelle wrote: You could do something like this with the help of itertools.ifilter: prime_gen = ifilter( lambda n, P=[]: all(n%p for p in P) and not P.append(n), count(2) ) Formidable! (both the English and French meanings) This is the most elegant, Sieve of Eratos