Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40697
| 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 | <davea@davea.name> |
| 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 <davea@davea.name> |
| 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 | <roy-572C99.22201106032013@70-1-84-166.pools.spcsdns.net> |
| In-Reply-To | <roy-572C99.22201106032013@70-1-84-166.pools.spcsdns.net> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2975.1362627544.2939.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-06 22:20 -0500
Re: Interesting list() un-optimization Dave Angel <davea@davea.name> - 2013-03-06 22:38 -0500
Re: Interesting list() un-optimization Tim Chase <python.list@tim.thechases.com> - 2013-03-06 21:57 -0600
Re: Interesting list() un-optimization Kev Dwyer <kevin.p.dwyer@gmail.com> - 2013-03-07 07:31 +0000
Re: Interesting list() un-optimization Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-07 11:22 +0000
Re: Interesting list() un-optimization Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 09:00 -0700
Re: Interesting list() un-optimization Christian Heimes <christian@python.org> - 2013-03-07 17:20 +0100
Re: Interesting list() un-optimization Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 10:31 -0700
Re: Interesting list() un-optimization Stefan Behnel <stefan_ml@behnel.de> - 2013-03-07 20:19 +0100
Re: Interesting list() un-optimization Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 13:26 -0700
Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-07 15:29 -0500
Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-07 15:34 -0500
Re: Interesting list() un-optimization Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-07 20:41 +0000
Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-07 17:53 -0500
Re: Interesting list() un-optimization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-08 17:59 +0000
Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-08 13:45 -0500
Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-10 09:05 -0400
Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-10 17:39 -0400
Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-10 18:34 -0400
Re: Interesting list() un-optimization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-10 23:35 +0000
Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-10 19:50 -0400
csiph-web