Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'argument': 0.04; 'method.': 0.05; 'seemed': 0.07; 'works.': 0.07; 'be:': 0.09; 'counting': 0.09; 'indeed,': 0.09; 'normally,': 0.09; 'prevents': 0.09; 'iterable': 0.16; 'iterating': 0.16; 'query,': 0.16; 'resizing': 0.16; 'roy': 0.16; 'stumbled': 0.16; 'subject:optimization': 0.16; 'trivia': 0.16; 'wrote:': 0.17; 'specifies': 0.17; 'memory': 0.18; 'trying': 0.21; 'bit': 0.21; 'assumes': 0.22; 'needed.': 0.23; 'second': 0.24; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'used,': 0.27; "doesn't": 0.28; 'subject:list': 0.28; 'initial': 0.28; 'falls': 0.29; 'obj': 0.29; 'optional': 0.29; 'knows': 0.30; 'query': 0.30; 'lists': 0.31; 'code': 0.31; 'asked': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'list': 0.35; 'faster': 0.35; 'pm,': 0.35; 'list.': 0.35; 'but': 0.36; "didn't": 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'subject:-': 0.40; 'first': 0.61; 'back': 0.62; 'time,': 0.62; 'information': 0.63; 'results': 0.65; 'today.': 0.69; 'received:74.208': 0.71; 'smith': 0.71; 'discover': 0.72; 'received:74.208.4.194': 0.84; 'items,': 0.91 Date: Wed, 06 Mar 2013 22:38:58 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Interesting list() un-optimization References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:wK2IR3/J9OyuBoA6Jd7Csav5tRGbDmtdwQzWJEFeDQY Edjrxno9an8r67EXIgAgHqpvAunR1hV5Qz3ikfzRKa4L6isLAr mMOCzKSB2fx2RCPdpXVdFKBUur2+lMPdbP1ZT/p2fbR1IekizN EFx9bMyfmOVjSdz3SgU1+AzX6tizOFzrO2Ck6CDgup7TqiEAjA IUiNU0qr97RnNB3APnA4b9KJBIP23pSUwJYwurwFix6B6u0evH lKoRrcq7p0HPgwXY8hTo2XvhQudGknouFIrAYlGxz/4qNxiJjE Xv0MUk7kqLlT7eCiblCvlbUWpxusYsdjfr7o+P+tbjcVo7J8w= = 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362627544 news.xs4all.nl 6869 [2001:888:2000:d::a6]:57928 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40697 On 03/06/2013 10:20 PM, Roy Smith wrote: > I stumbled upon an interesting bit of trivia concerning lists and list > comprehensions today. > > We use mongoengine as a database model layer. A mongoengine query > returns an iterable object called a QuerySet. The "obvious" way to > create a list of the query results would be: > > my_objects = list(my_query_set) > > and, indeed, that works. But, then I found this code: > > my_objects = [obj for obj in my_query_set] > > which seemed a bit silly. I called over the guy who wrote it and asked > him why he didn't just write it using list(). I was astounded when it > turned out there's a good reason! > > Apparently, list() has an "optimization" where it calls len() on its > argument to try and discover the number of items it's going to put into > the list. Presumably, list() uses this information to pre-allocate the > right amount of memory the first time, without any resizing. If len() > fails, it falls back to just iterating and resizing as needed. > Normally, this would be a win. > > The problem is, QuerySets have a __len__() method. Calling it is a lot > faster than iterating over the whole query set and counting the items, > but it does result in an additional database query, which is a lot > slower than the list resizing! Writing the code as a list comprehension > prevents list() from trying to optimize when it shouldn't! > That is very interesting. list() assumes the __len__() method would be very quick. Perhaps list() should take an optional second argument that specifies the initial length to allocate. That way code that either doesn't want __len__() to be used, or that already knows a reasonable number to use, can supply the value to preallocate. -- DaveA