Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'iterate': 0.09; 'lst': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; ':-)': 0.13; '"calling': 0.16; '-tkc': 0.16; 'foo():': 0.16; 'foo(object):': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'rewritten': 0.16; 'roy': 0.16; 'stumbled': 0.16; 'subject:optimization': 0.16; 'trivia': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'bit': 0.21; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'testing': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'subject:list': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'call.': 0.30; 'lists': 0.31; 'print': 0.32; 'version': 0.34; 'agree': 0.34; 'list': 0.35; 'charset:us- ascii': 0.36; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'shows': 0.38; 'performance': 0.39; 'little': 0.39; 'subject:-': 0.40; 'today.': 0.69; 'smith': 0.71; 'costly': 0.84; 'received:50.22': 0.84; 'angel': 0.93 Date: Wed, 6 Mar 2013 21:57:35 -0600 From: Tim Chase To: Roy Smith Subject: Re: Interesting list() un-optimization In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: 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: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362628560 news.xs4all.nl 6982 [2001:888:2000:d::a6]:36657 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40699 On 2013-03-06 22:20, Roy Smith wrote: > I stumbled upon an interesting bit of trivia concerning lists and > list comprehensions today. I agree with Dave Angel that this is interesting. 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 :-) -tkc 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) print "Ensuring we can iterate over it:" for x in Foo(): print x print "\nJust call list():" lst = list(Foo()) print lst print "\nCall list(iter())" lst = list(iter(Foo())) print lst