Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27804
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
|---|---|
| From | Ramchandra Apte <maniandram01@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Filter versus comprehension (was Re: something about split()???) |
| Date | Fri, 24 Aug 2012 07:44:27 -0700 (PDT) |
| Organization | http://groups.google.com |
| Lines | 125 |
| Message-ID | <960e4798-745b-4e9b-aedb-14aae986d086@googlegroups.com> (permalink) |
| References | <CADYZVBDSGb=KL+KgvEG1j1jFnqjKifk6RBJd8C0mKk83rR+_HA@mail.gmail.com> <581D4160B04DF541A763BB8BB60E8CC6A747B70A66@PDC-MAIL-CMS01.ubisoft.org> <CAExgZOh=_Mr0WR4ypXV70s9MUbDHbeppyAq=Nn2yy2NCsws7jQ@mail.gmail.com> <502A865E.4030504@sequans.com> <CAExgZOiHi-mE1mVvxYOST5RDbosDn3fP1X2nxkyDiKk=0ajjSw@mail.gmail.com> <CADYZVBCZZ3cAHGDe=wyi62Hg9+qU_r2U8F+85gHEOzs0Ekv1BA@mail.gmail.com> <k11ro8$cer$1@ger.gmane.org> <k121ob$lp9$1@ger.gmane.org> <mailman.3665.1345653816.4697.python-list@python.org> |
| NNTP-Posting-Host | 122.172.247.246 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Trace | posting.google.com 1345819468 2059 127.0.0.1 (24 Aug 2012 14:44:28 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Fri, 24 Aug 2012 14:44:28 +0000 (UTC) |
| Cc | python-list@python.org |
| In-Reply-To | <mailman.3665.1345653816.4697.python-list@python.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=122.172.247.246; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD |
| User-Agent | G2/1.0 |
| Xref | csiph.com comp.lang.python:27804 |
Show key headers only | View raw
On Wednesday, 22 August 2012 22:13:04 UTC+5:30, Terry Reedy wrote:
> On 8/22/2012 3:30 AM, Mark Lawrence wrote:
>
> > On 22/08/2012 06:46, Terry Reedy wrote:
>
> >> On 8/21/2012 11:43 PM, mingqiang hu wrote:
>
> >>> why filter is bad when use lambda ?
>
> >>
>
> >> Inefficient, not 'bad'. Because the equivalent comprehension or
>
> >> generator expression does not require a function call.
>
>
>
> for each item in the iterable.
>
>
>
> > A case of premature optimisation? :)
>
>
>
> No, as regards my post. I simply made a factual statement without
>
> advocating a particular action.
>
>
>
> filter(lambda x: <expr>, iterable)
>
> (x for x in iterable if <expr>)
>
>
>
> both create iterators that produce the items in iterable such that
>
> bool(<expr>) is true. The following, with output rounded, shows
>
> something of the effect of the extra function call.
>
>
>
> >>> timeit.timeit("list(i for i in ranger if False)", "ranger=range(0)")
>
> 0.91
>
> >>> timeit.timeit("list(i for i in ranger if False)", "ranger=range(20)")
>
> 1.28
>
> >>> timeit.timeit("list(filter(lambda i: False, ranger))",
>
> "ranger=range(0)")
>
> 0.83
>
> >>> timeit.timeit("list(filter(lambda i: False, ranger))",
>
> "ranger=range(20)")
>
> 2.60
>
>
>
> Simply keeping true items is faster with filter -- at least on my
>
> particular machine with 3.3.0b2.
>
>
>
> >>> timeit.timeit("list(filter(None, ranger))", "ranger=range(20)")
>
> 1.03
>
>
>
> Filter is also faster if the expression is a function call.
>
>
>
> >>> timeit.timeit("list(filter(f, ranger))", "ranger=range(20);
>
> f=lambda i: False")
>
> 2.5033614114454394
>
> >>> timeit.timeit("list(i for i in ranger if f(i))", "ranger=range(20);
>
> f=lambda i: False")
>
> 3.2394095327040304
>
>
>
> ---
>
> Perhaps or even yes as regards the so-called rule 'always use
>
> comprehension'. If one prefers filter as more readable, if one only
>
> wants to keep true items, if the expression is a function call, if
>
> evaluating the expression takes much more time than the extra function
>
> call so the latter does not matter, if the number of items is few enough
>
> that the extra time does not matter, then the rule is not needed or even
>
> wrong.
>
>
>
> So I think PyLint should be changed to stop its filter fud.
>
>
>
> --
>
> Terry Jan Reedy
When filtering for true values, filter(None,xxx) can be used
Your examples with lambda i:False are unrealistic - you are comparing `if False` vs <lambda function>(xx) - function call vs boolean check
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Filter versus comprehension (was Re: something about split()???) Terry Reedy <tjreedy@udel.edu> - 2012-08-22 12:43 -0400
Re: Filter versus comprehension (was Re: something about split()???) Ramchandra Apte <maniandram01@gmail.com> - 2012-08-24 07:44 -0700
Re: Filter versus comprehension (was Re: something about split()???) Terry Reedy <tjreedy@udel.edu> - 2012-08-24 12:04 -0400
Re: Filter versus comprehension (was Re: something about split()???) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-24 14:29 -0400
Re: Filter versus comprehension (was Re: something about split()???) Walter Hurry <walterhurry@lavabit.com> - 2012-08-24 19:03 +0000
Re: Filter versus comprehension (was Re: something about split()???) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-24 17:56 -0400
Re: Filter versus comprehension (was Re: something about split()???) Walter Hurry <walterhurry@lavabit.com> - 2012-08-24 22:55 +0000
Re: Filter versus comprehension (was Re: something about split()???) Terry Reedy <tjreedy@udel.edu> - 2012-08-24 18:03 -0400
Re: Filter versus comprehension (was Re: something about split()???) Emile van Sebille <emile@fenx.com> - 2012-08-24 15:15 -0700
Re: Filter versus comprehension (was Re: something about split()???) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-24 23:28 +0100
Re: Filter versus comprehension (was Re: something about split()???) Ned Deily <nad@acm.org> - 2012-08-24 15:36 -0700
Re: Filter versus comprehension (was Re: something about split()???) Ned Deily <nad@acm.org> - 2012-08-24 15:39 -0700
Re: Filter versus comprehension (was Re: something about split()???) David Robinow <drobinow@gmail.com> - 2012-08-25 08:57 -0400
Re: Filter versus comprehension (was Re: something about split()???) Tim Golden <mail@timgolden.me.uk> - 2012-08-25 16:31 +0100
Re: Filter versus comprehension (was Re: something about split()???) Ramchandra Apte <maniandram01@gmail.com> - 2012-08-24 07:44 -0700
csiph-web