Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Re: Chaining 501 generators breaks everything?
Jonathan Gardner
2/19/2010 9:48:00 PM
On Fri, Feb 19, 2010 at 11:47 AM, Andrey Fedorov <anfedorov@gmail.com> wrote:
> I implemented a Sieve of Eratosthenes primes algorithm using generators:
>
>
http://gist.github....
>
> This code which chains together 500 generators works fine (~1/20th of a
> second) on my laptop.
>
You may want a more traditional approach. Until Python gets tail
recursion (doubtful), this won't work very well on any platform.
#!/usr/bin/env python
from itertools import islice, count
from time import time
def primes():
seen = []
for i in count(2):
for s in seen:
if i%s == 0:
break
else: # Run if the for loop doesn't break
seen.append(i)
yield i
start = time()
for i in islice(primes(), 0, 10000):
print i
print time() - start
--
Jonathan Gardner
jgardner@jonathangardner.net
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Chaining 501 generators breaks everything?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password