Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'subject:skip:c 10': 0.07; 'subject:question': 0.08; 'missed': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'oct': 0.16; 'reminding': 0.16; 'rings': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; "i'd": 0.22; 'kevin': 0.23; 'seems': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'cc:addr:gmail.com': 0.27; 'cc:2**2': 0.27; 'correct': 0.28; 'received:209.85.212': 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; 'operations': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'list': 0.35; 'faster': 0.35; 'from:addr:googlemail.com': 0.35; 'said,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'too': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'help': 0.40; 'most': 0.61; 'real': 0.61; 'sum': 0.66; '10%': 0.81; 'improvement': 0.84; 'received:209.85.212.56': 0.91; 'rusi': 0.91; 'angel': 0.93 Newsgroups: comp.lang.python Date: Wed, 17 Oct 2012 07:45:50 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: <507E145E.9060008@davea.name> <61d76114-3021-4a3b-9943-1e81147d3ce1@6g2000pbh.googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: list comprehension question From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: rusi , d@davea.name, python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350485158 news.xs4all.nl 6916 [2001:888:2000:d::a6]:43158 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31514 Dave Angel=E6=96=BC 2012=E5=B9=B410=E6=9C=8817=E6=97=A5=E6=98=9F=E6=9C=9F= =E4=B8=89UTC+8=E4=B8=8B=E5=8D=8810=E6=99=8237=E5=88=8601=E7=A7=92=E5=AF=AB= =E9=81=93=EF=BC=9A > On 10/17/2012 10:06 AM, rusi wrote: >=20 > > On Oct 17, 5:33 pm, Dave Angel wrote: >=20 > >> On 10/17/2012 12:43 AM, Kevin Anthony wrote:> Is it not true that list= comprehension is much faster the the for loops? >=20 > >> >=20 > >>> If it is not the correct way of doing this, i appoligize. >=20 > >>> Like i said, I'm learing list comprehension. >=20 > >> list comprehensions CAN be much faster, but not necessarily. The most >=20 > >> complex a loop, the less likely it'll help much. >=20 > > One-lining the comprehension seems to make a difference of about 10% >=20 > > out here. Maybe Ive missed something? Seems too large=C3=AF=C2=BF=C2=BD >=20 > > >=20 > > # My original suggestion >=20 > > def dot(p,q): return sum (x*y for x,y in zip(p,q)) >=20 > > def transpose(m): return zip(*m) >=20 > > def mm(a,b): return mmt(a, transpose(b)) >=20 > > def mmt(a,b): return [[dot(ra, rb) for rb in b] for ra in a] >=20 > > >=20 > > # One-liner (Thanks Hans for reminding me of sum) >=20 > > >=20 > > def mm1(a,b): return [[sum([x*y for x,y in zip(ra,rb)]) for rb in >=20 > > zip(*b)] for ra in a] >=20 > > >=20 > >>>> t1=3DTimer("res=3Dmm1(m,m)", setup=3D"from __main__ import mm1, m") >=20 > >>>> t1.timeit(1000) >=20 > > 12.276363849639893 >=20 > >>>> t0=3DTimer("res=3Dmm(m,m)", setup=3D"from __main__ import mm, m") >=20 > >>>> t0.timeit(1000) >=20 > > 13.453603029251099 >=20 >=20 >=20 > And I'd wager all the improvement is in the inner loop, the dot() functio= n. >=20 >=20 >=20 >=20 >=20 > --=20 >=20 >=20 >=20 > DaveA Thanks for the tips of matrix operations over some fields or rings=20 other than the real field and the complex field.