Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87625

Re: Dict comprehensions - improvement to docs?

From "Frank Millman" <frank@chagford.com>
Subject Re: Dict comprehensions - improvement to docs?
Date 2015-03-17 08:44 +0200
References (1 earlier) <87fv95fom0.fsf@jester.gateway.sonic.net> <mailman.427.1426494675.21433.python-list@python.org> <87wq2hfibu.fsf@jester.gateway.sonic.net> <me664q$hon$1@ger.gmane.org> <CALwzid=u19YMkfJBhbLZi1qh2U9UK4ohY5wco1zO-i3T5AtyOA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.490.1426574679.21433.python-list@python.org> (permalink)

Show all headers | View raw


"Ian Kelly" <ian.g.kelly@gmail.com> wrote in message 
news:CALwzid=u19YMkfJBhbLZi1qh2U9UK4ohY5wco1zO-i3T5AtyOA@mail.gmail.com...
> On Mon, Mar 16, 2015 at 3:01 AM, Frank Millman <frank@chagford.com> wrote:
>> C:\>python -m timeit -s "x = range(65, 91); y = (chr(z) for z in x)"
>> "dict(zip(x, y))"
>> 100000 loops, best of 3: 11.9 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)}"
>> 100000 loops, best of 3: 7.24 usec per loop
>
> Since the setup code is only run once, the generator expression used
> for y is only iterated over once. On every subsequent loop, zip is
> producing an empty result. So this measurement is really just
> capturing the overhead of the dict construction. Compare:
>
> $ python3 -m timeit -s "x = range(65, 91); y = (chr(z) for z in x)"
> "dict(zip(x,y))"
> 1000000 loops, best of 3: 0.9 usec per loop
> $ python3 -m timeit -s "x = range(65, 91); y = [chr(z) for z in x]"
> "dict(zip(x,y))"
> 100000 loops, best of 3: 2.69 usec per loop
> $ python3 -m timeit -s "x = range(65, 91); y = (chr(z) for z in x)"
> "{a:b for a,b in zip(x,y)}"
> 1000000 loops, best of 3: 0.837 usec per loop
> $ python3 -m timeit -s "x = range(65, 91); y = [chr(z) for z in x]"
> "{a:b for a,b in zip(x,y)}"
> 100000 loops, best of 3: 2.67 usec per loop

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

Or to use Paul's original example -

C:\>python -m timeit "d = dict((k, v) for k, v in [('name', 'paul'), 
('language', 'python')])
100000 loops, best of 3: 16.6 usec per loop

C:\>python -m timeit "d = {k: v for k, v in [('name', 'paul'), ('language', 
'python')]}
100000 loops, best of 3: 5.2 usec per loop

It seems that a dict comp is noticeably faster.

Does this sound right, or are there other factors I should be taking into 
account?

Frank


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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