itertools candidate: warehouse()

Alex Martelli aleaxit at yahoo.com
Tue Oct 19 17:53:59 EDT 2004


Raymond Hettinger <vze4rx4y at verizon.net> wrote:

> [Alex Martelli]
> > Stylistically, I prefer
> >     iter(random.random, None)
> > using the 2-args form of the built-in iter, to
> >     itertools.starmap(random.random, itertools.repeat(()))
> 
> FWIW, I prefer loading the itertools recipes so I can write:
> 
>      repeatfunc(random.random)
> 
> IMO, that is plainer than both the iter() and starmap() versions.

I agree, but I cannot distribute Python code containing that, since the
itertools recipes aren't part of the stdlib.


> > However, itertools IS a speed demon...:
> >
> > kallisti:~/cb alex$ python -m timeit -s 'import random, itertools as it'
> > \     > 'list(it.islice(iter(random.random, None), 666))'
> > 1000 loops, best of 3: 884 usec per loop
> >
> > kallisti:~/cb alex$ python -m timeit -s 'import random, itertools as it'
> > \     > 'list(it.islice(it.starmap(random.random, it.repeat(())), 666))'
> > 1000 loops, best of 3: 407 usec per loop
> 
> Also time:
> 
>     iter(random.random, 1.0)
> 
> IIRC, floats compare to each other faster than a float to None.

Excellent observation!  They do indeed:

kallisti:~/cb/little_neat_things alex$ python -m timeit -s 'import
random, itertools as it' 'list(it.islice(iter(random.random, 1.0),
666))'
1000 loops, best of 3: 564 usec per loop


Alex



More information about the Python-list mailing list