Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31514
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-10-17 07:45 -0700 |
| References | (1 earlier) <507E145E.9060008@davea.name> <CA+C4C6cjha4bMZ9gyx5fTp0c_nfFQxRGu31dGNMjgHQBu73=ig@mail.gmail.com> <mailman.2341.1350477185.27098.python-list@python.org> <61d76114-3021-4a3b-9943-1e81147d3ce1@6g2000pbh.googlegroups.com> <mailman.2348.1350484621.27098.python-list@python.org> |
| Subject | Re: list comprehension question |
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
| Message-ID | <mailman.2349.1350485158.27098.python-list@python.org> (permalink) |
Dave Angel於 2012年10月17日星期三UTC+8下午10時37分01秒寫道:
> On 10/17/2012 10:06 AM, rusi wrote:
>
> > On Oct 17, 5:33 pm, Dave Angel <d...@davea.name> 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
Thanks for the tips of matrix operations over some fields or rings
other than the real field and the complex field.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: list comprehension question Dave Angel <d@davea.name> - 2012-10-17 08:32 -0400
Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 07:06 -0700
Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 07:33 -0700
Re: list comprehension question Dave Angel <d@davea.name> - 2012-10-17 10:36 -0400
Re: list comprehension question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-17 07:45 -0700
Re: list comprehension question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-17 07:45 -0700
Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 07:50 -0700
Re: list comprehension question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-17 08:06 -0700
csiph-web