yield iteration
bearophile
bearophileHUGS at lycos.com
Sun Jul 29 08:42:33 PDT 2012
Tobias Pankrath:
> Does this spawn linear many generators?
I have written this little test code, Python2.6+:
count = 0
def A006068():
global count
count += 1
yield 0
for x in A006068():
if x & 1:
yield 2 * x + 1
yield 2 * x
else:
if x:
yield 2 * x
yield 2 * x + 1
from itertools import islice
def main():
global count
for p in xrange(15):
n = 2 ** p
count = 0
print sum(1 for _ in islice(A006068(), 0, n)), count
main()
The output seems to show that it spawns only logarithmically:
1 1
2 2
4 3
8 4
16 5
32 6
64 7
128 8
256 9
512 10
1024 11
2048 12
4096 13
8192 14
16384 15
Bye,
bearophile
More information about the Digitalmars-d
mailing list