Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87668
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Subject | Re: Dict comprehensions - improvement to docs? |
| Date | 2015-03-18 10:01 +0200 |
| References | (3 earlier) <87wq2hfibu.fsf@jester.gateway.sonic.net> <me664q$hon$1@ger.gmane.org> <CALwzid=u19YMkfJBhbLZi1qh2U9UK4ohY5wco1zO-i3T5AtyOA@mail.gmail.com> <me8ige$kjp$1@ger.gmane.org> <CALwzidmNDcSvER7S6inEaVZA=DHUrDX1KzL-WRVwhd=o3_LvWA@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.513.1426665702.21433.python-list@python.org> (permalink) |
"Ian Kelly" <ian.g.kelly@gmail.com> wrote in message
news:CALwzidmNDcSvER7S6inEaVZA=DHUrDX1KzL-WRVwhd=o3_LvWA@mail.gmail.com...
> On Tue, Mar 17, 2015 at 12:44 AM, Frank Millman <frank@chagford.com>
> wrote:
>> Thanks for the explanation. I'll try not to make that mistake again.
>>
>> However, to go back to the original example, we want to compare a dict
>> comprehension with a dict() constructor using a generator expression.
>>
>> Let's see if I have got this one right -
>>
>> C:\>python -m timeit -s "x=range(65, 91); y=[chr(z) for z in x]"
>> "dict((a,
>> b) for a, b in zip(x, y))"
>> 10000 loops, best of 3: 49.6 usec per loop
>>
>> C:\>python -m timeit -s "x=range(65, 91); y=[chr(z) for z in x]" "{a: b
>> for
>> a, b in zip(x, y)}"
>> 10000 loops, best of 3: 25.8 usec per loop
>
> Why did you revert back to the no-op generator expression in the first
> case instead of the more efficient dict(zip(x, y))?
Firstly, I just want to emphasise that I am not trying to prove anything
here, I am trying to learn something.
Why do you call it a no-op? I understood your previous point that a
generator in the setup statement is exhausted after the first execution, but
this is in the run-time statement, so I thought it would be executed every
time.
If one does not need any 'comprehension' functionality, I agree that
dict(zip(x, y)) is the way to go.
However, if you do need a comprehension, Paul questioned why a special dict
comprehension was introduced when you can easily use a generator expression.
I ran the timing tests to compare the two, and I was surprised to see such a
difference. However, you have already pointed out one case where I was not
comparing apples and apples, and it is quite possible that I have stumbled
across another one.
I read Lele's post where he pointed out that part of the difference is
explained by the fact that dict() involves a function call, whereas {} does
not. However, this does not seem to explain the entire difference.
Frank
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-16 07:25 +0200
Re: Dict comprehensions - improvement to docs? Paul Rubin <no.email@nospam.invalid> - 2015-03-15 23:22 -0700
Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-16 10:30 +0200
Re: Dict comprehensions - improvement to docs? Paul Rubin <no.email@nospam.invalid> - 2015-03-16 01:38 -0700
Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-16 11:01 +0200
Re: Dict comprehensions - improvement to docs? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-16 09:57 -0600
Re: Dict comprehensions - improvement to docs? Paul Rubin <no.email@nospam.invalid> - 2015-03-16 15:41 -0700
Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-17 08:44 +0200
Re: Dict comprehensions - improvement to docs? Lele Gaifax <lele@metapensiero.it> - 2015-03-17 15:06 +0100
Re: Dict comprehensions - improvement to docs? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-17 09:48 -0600
Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-18 10:01 +0200
Re: Dict comprehensions - improvement to docs? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-18 02:39 -0600
csiph-web