Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.052 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'subject:skip:c 10': 0.07; 'subject:question': 0.08; 'missed': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'oct': 0.16; 'reminding': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; "i'd": 0.22; 'cc:2**0': 0.23; 'kevin': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'correct': 0.28; 'subject:list': 0.28; '>>>>': 0.29; 'faster,': 0.29; 'loop,': 0.29; "i'm": 0.29; 'maybe': 0.29; 'suggestion': 0.32; 'function.': 0.33; 'much.': 0.33; 'likely': 0.33; 'list': 0.35; 'faster': 0.35; 'said,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'but': 0.36; 'too': 0.36; 'subject:: ': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'most': 0.61; 'charset:windows-1252': 0.65; 'sum': 0.66; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; '10%': 0.81; 'improvement': 0.84; 'received:74.208.4.194': 0.84; 'rusi': 0.91; 'angel': 0.93 Date: Wed, 17 Oct 2012 10:36:38 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: rusi Subject: Re: list comprehension question References: <507E145E.9060008@davea.name> <61d76114-3021-4a3b-9943-1e81147d3ce1@6g2000pbh.googlegroups.com> In-Reply-To: <61d76114-3021-4a3b-9943-1e81147d3ce1@6g2000pbh.googlegroups.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:ks7v8B/gDr8HZ6+GGgMFYgznX6GqNTHWgeMEHmxW4rt hD5+Cem52htvkrbIfVBlssq2eq6rdOHqxlQz5bklBsqKS31c+S /1zf/auIrH4h5gZWoX9z71d1DsLThQ9eEIpDz1t1sU7afZXLUr 5+/bttJLIPPOL43u3S6tZdD5LZX+0NQEe513z7/N9D3SsH00gz O3FFTJ051vU4bIieSOEWMVTtRXY7MAg2qKqJiT533Qn34jcZlF 8rQsYQWXBoGIpvOeTUqdgDh2kT4Mjwcjg/tpD1d9YcdmA5Q2AP OTKxyEXquqN9uglZ2Ex8WMGhDgtMPvzWDB63bPqwDY5E+ARuA= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350484621 news.xs4all.nl 6939 [2001:888:2000:d::a6]:59410 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31512 On 10/17/2012 10:06 AM, rusi wrote: > On Oct 17, 5:33 pm, Dave Angel wrote: >> On 10/17/2012 12:43 AM, Kevin Anthony wrote:> Is it not true that list comprehension is much faster the the for loops? >> >>> If it is not the correct way of doing this, i appoligize. >>> Like i said, I'm learing list comprehension. >> list comprehensions CAN be much faster, but not necessarily. The most >> complex a loop, the less likely it'll help much. > One-lining the comprehension seems to make a difference of about 10% > out here. Maybe Ive missed something? Seems too large… > > # My original suggestion > def dot(p,q): return sum (x*y for x,y in zip(p,q)) > def transpose(m): return zip(*m) > def mm(a,b): return mmt(a, transpose(b)) > def mmt(a,b): return [[dot(ra, rb) for rb in b] for ra in a] > > # One-liner (Thanks Hans for reminding me of sum) > > def mm1(a,b): return [[sum([x*y for x,y in zip(ra,rb)]) for rb in > zip(*b)] for ra in a] > >>>> t1=Timer("res=mm1(m,m)", setup="from __main__ import mm1, m") >>>> t1.timeit(1000) > 12.276363849639893 >>>> t0=Timer("res=mm(m,m)", setup="from __main__ import mm, m") >>>> t0.timeit(1000) > 13.453603029251099 And I'd wager all the improvement is in the inner loop, the dot() function. -- DaveA