xrange

Bryan belred1 at yahoo.com
Sun Jun 22 12:26:59 EDT 2003


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3EF5D5E2.DF5D9FCB at engcorp.com...
> Bryan wrote:
> >
> > in 2.3, what is the best way to write this:
> >   for i in xrange(100): print 'Hello'
> >
> > i used timeit and it seems that repeat is about 10% faster.
>
> Is such a small performance difference more important to you than
> backwards compatibility?
>
> I would choose "for i in xrange()" still, merely because of the
> number of people still using Python 2.2 and before, but I'm not
> one who cares much about trivial optimizations...
>
> (I realize you might be using 2.3-only features, but I don't think
> that automatically means you should abandon readability, by dumping
> a well-known idiom with a merely possibly-to-become-idiomatic form.)
>
> -Peter

i was trying to think in general terms,  not just a simple 100 iterations.
of course for backwards compatibility xrange wins there.  to me both xrange
and repeat are similarly readable.  i guess i got a bit confused because
python now has two obviously correct way of doing something, and for the
rest of my python life i have to always decide which one to use.







More information about the Python-list mailing list