Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'method.': 0.05; 'feasible.': 0.09; "object's": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'itself.': 0.11; '"calling': 0.16; 'foo(object):': 0.16; 'message-id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'rewritten': 0.16; 'roy': 0.16; 'skips': 0.16; 'stumbled': 0.16; 'subject:optimization': 0.16; 'trivia': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'tim': 0.18; 'bit': 0.21; 'defined': 0.22; 'seems': 0.23; 'testing': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.28; 'subject:list': 0.28; 'chase': 0.29; 'received:132': 0.29; 'workaround': 0.29; 'writes:': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'call.': 0.30; 'lists': 0.31; 'print': 0.32; 'cases,': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'self': 0.34; 'list': 0.35; 'faster': 0.35; 'received:org': 0.36; 'method': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'best,': 0.37; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'shows': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'little': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; 'today.': 0.69; 'smith': 0.71; 'costly': 0.84; 'hood': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Wolfgang Maier Subject: Re: Interesting list() un-optimization Date: Thu, 7 Mar 2013 11:22:56 +0000 (UTC) References: <20130306215735.649932ee@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 132.230.1.31 (Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0) 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: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362655396 news.xs4all.nl 6968 [2001:888:2000:d::a6]:44617 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40748 Tim Chase tim.thechases.com> writes: > On 2013-03-06 22:20, Roy Smith wrote: > > I stumbled upon an interesting bit of trivia concerning lists and > > list comprehensions today. > > A little testing > shows that this can be rewritten as > > my_objects = list(iter(my_query_set)) > > which seems to then skip the costly __len__ call. Performance geeks > are welcome to time it against the list-comprehension version > > class Foo(object): > def __init__(self): > self.items = range(10) > def __iter__(self): > return iter(self.items) > def __len__(self): > print "Calling costly __len__" > return len(self.items) > Well, it skips the costly len() call because your iter(Foo()) returns iter(range()) under the hood and list() uses that object's __len__() method. In most cases, such a workaround will not be feasible. Why should iter(QuerySet()) have a faster __len__() method defined than QuerySet() itself. Most likely, iter(QuerySet()) just returns self anyway? Best, Wolfgang