Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'false,': 0.07; 'subject:skip:c 10': 0.07; 'lawrence': 0.09; 'terry': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; '1.03': 0.16; 'advocating': 0.16; 'boolean': 0.16; 'evaluating': 0.16; 'iterable': 0.16; 'iterable)': 0.16; 'iterators': 0.16; 'lambda': 0.16; 'reedy': 0.16; 'subject:Filter': 0.16; 'subject:versus': 0.16; 'wrote:': 0.17; 'comparing': 0.17; 'examples': 0.18; 'jan': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'filtering': 0.22; 'latter': 0.22; 'wednesday,': 0.22; 'cc:2**0': 0.23; 'statement': 0.23; 'cc:no real name:2**0': 0.24; 'machine': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; '---': 0.26; 'am,': 0.27; 'so-called': 0.29; 'no,': 0.29; 'call.': 0.30; 'function': 0.30; 'true.': 0.33; 'changed': 0.34; 'received:google.com': 0.34; 'needed': 0.35; 'faster': 0.35; 'filter': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'should': 0.36; 'subject: (': 0.36; 'enough': 0.36; 'bad': 0.37; 'does': 0.37; 'why': 0.37; 'item': 0.37; 'skip:3 10': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'shows': 0.38; 'takes': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'more': 0.63; 'august': 0.66; 'action.': 0.84; 'prefers': 0.84; 'premature': 0.84; 'items,': 0.91 Newsgroups: comp.lang.python Date: Fri, 24 Aug 2012 07:44:27 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.172.247.246; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD References: <581D4160B04DF541A763BB8BB60E8CC6A747B70A66@PDC-MAIL-CMS01.ubisoft.org> <502A865E.4030504@sequans.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 122.172.247.246 MIME-Version: 1.0 Subject: Re: Filter versus comprehension (was Re: something about split()???) From: Ramchandra Apte To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 125 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345819477 news.xs4all.nl 6963 [2001:888:2000:d::a6]:42427 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27805 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: , iterable) > > (x for x in iterable if ) > > > > both create iterators that produce the items in iterable such that > > bool() 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 (xx) - function call vs boolean check