Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '16,': 0.03; 'subsequent': 0.05; 'python3': 0.07; 'constructor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'wrote': 0.14; 'comp': 0.16; 'dict': 0.16; 'expression.': 0.16; 'iterated': 0.16; 'measurement': 0.16; 'once.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.18; 'producing': 0.19; 'result.': 0.19; 'seems': 0.21; 'example': 0.22; 'mon,': 0.24; 'compare': 0.26; 'header:X -Complaints-To:1': 0.27; 'am,': 0.29; 'code': 0.31; '100000': 0.31; 'once,': 0.31; 'overhead': 0.31; 'run': 0.32; 'there': 0.35; 'really': 0.36; 'thanks': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'expression': 0.60; 'back': 0.62; 'zip': 0.64; 'taking': 0.65; 'mar': 0.68; '10000': 0.68; 'account?': 0.68; 'frank': 0.68; 'sound': 0.68; '2015': 0.84; 'compare:': 0.84; 'dict()': 0.84; 'faster.': 0.84; "paul's": 0.84; '5.2': 0.91; 'mistake': 0.91; 'factors': 0.97 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Dict comprehensions - improvement to docs? Date: Tue, 17 Mar 2015 08:44:30 +0200 References: <87fv95fom0.fsf@jester.gateway.sonic.net> <87wq2hfibu.fsf@jester.gateway.sonic.net> X-Gmane-NNTP-Posting-Host: 197.89.67.82 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426574679 news.xs4all.nl 2975 [2001:888:2000:d::a6]:46053 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87625 "Ian Kelly" wrote in message news:CALwzid=u19YMkfJBhbLZi1qh2U9UK4ohY5wco1zO-i3T5AtyOA@mail.gmail.com... > On Mon, Mar 16, 2015 at 3:01 AM, Frank Millman 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