itertools candidate: warehouse()

Raymond Hettinger vze4rx4y at verizon.net
Tue Oct 19 17:39:00 EDT 2004


[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.


> 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.


Raymond Hettinger





More information about the Python-list mailing list