generators : random shootout

GerritM gmuller at worldonline.nl
Sat Jan 12 14:24:56 EST 2002


"David Eppstein" <eppstein at ics.uci.edu> schreef in bericht
news:eppstein-927355.15502311012002 at news.service.uci.edu...
> In article <pan.2002.01.11.17.12.23.738306.2860 at uchicago.edu>,
>  Ben Wolfson <wolfson at uchicago.edu> wrote:
>
> > Changing this to
> > for i in zip(gr, xrange(1, N)):
> >     pass
> >
> > speeds it up a bit.
>
> What's the point of null-looping over the list made by zip?
> And did you really want to iterate exactly N-1 times?
> Why not just
>     zip(gr,xrange(N))
> ?
> --
> David Eppstein       UC Irvine Dept. of Information & Computer Science
> eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/

I did not yet look at iterators at all, therefor I like this thread a lot!
Apparantly the strange construction with the zip is used in order to get N
iterations of gr. Does some more elegant solution exist to get N iterations,
for instance:
gr[1:N]
gr.getiterations(100)

Regards Gerrit

P.S.,
the current "improvement" of the shootout version is worse in at least 2
aspects:
- run time performance (the original concern)
- the lines of code metric (original 25 excluding comments and empty lines,
versus 27 in the improved version). The increase is partially artificial
from __future__ import ... and the yield statement uses an additional
variable, where the original directly returns a value via an expression.
Nevertheless I think that iterators are only qualified as an improvement if
the line count decreases, due to increased expressiveness. The improvement
of David could at least gain one more line :-)

--
www.extra.research.philips.com/natlab/sysarch






More information about the Python-list mailing list