Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34746
| References | <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> <CALwzidnsEhQivtY9aNrmr4On74CG4ChAapy-8qupBUM=txdMrQ@mail.gmail.com> <mailman.804.1355350861.29569.python-list@python.org> <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2012-12-13 12:14 +1100 |
| Subject | Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.814.1355361251.29569.python-list@python.org> (permalink) |
On Thu, Dec 13, 2012 at 11:30 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Wed, 12 Dec 2012 17:20:53 -0500, Dave Cinege wrote:
>> To me for i in range(len(l)) seems like simpler, faster, tighter code
>> for this now.
>
> * the version with enumerate makes the intent more clear: since we
> care about looping over the items, we should iterate over the
> items directly, not over their indices;
To add to this: Using enumerate gives the possibility (don't know if
any current interpreter takes advantage or not, but a future one
certainly could) that the enumerate() call could be optimized out.
Yes, it's theoretically possible that someone could redefine
enumerate, which would be broken by such an optimization. But all it'd
take is some kind of directive-based optimization and voila, safe
performance improvements.
Example code used:
>>> def foo(x):
for i,val in enumerate(x):
print("x[%d] = %s"%(i,str(val)))
>>> def foo(x):
for i in range(len(x)):
val=x[i]
print("x[%d] = %s"%(i,str(val)))
A simple look at dis.dis() for the above two functions disproves the
"faster". Steven has already disproven the "simpler" and "tighter".
(I would like, though, to see a key-providing iteration as a standard
feature. Looking at dis.dis() for the above examples and also at a
simple iteration over a dictionary's .items(), I'm seeing what looks
like a lot of effort to deal with the fact that iterators return a
stream of items, rather than a stream of keys and values. But it's
probably not worth changing now.)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Dave Cinege <dave@cinege.com> - 2012-12-12 17:20 -0500
Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-13 00:30 +0000
Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Chris Angelico <rosuav@gmail.com> - 2012-12-13 12:14 +1100
Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Dave Cinege <dave@cinege.com> - 2012-12-12 20:30 -0500
Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Terry Reedy <tjreedy@udel.edu> - 2012-12-12 20:36 -0500
ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes - v20121212 Dave Cinege <dave@cinege.com> - 2012-12-12 20:42 -0500
csiph-web