Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41038

Re: Interesting list() un-optimization

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'method.': 0.05; 'versions.': 0.07; 'convenience': 0.09; 'counting': 0.09; 'models.': 0.09; 'prevents': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'terry': 0.09; 'def': 0.10; 'downside': 0.16; 'iterating': 0.16; 'query,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:optimization': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'solution.': 0.18; 'trying': 0.21; 'logical': 0.22; 'defined': 0.22; 'work.': 0.23; "i've": 0.23; 'seems': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'header:X -Complaints-To:1': 0.28; 'this?': 0.28; 'subject:list': 0.28; 'methods.': 0.29; 'respects': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'query': 0.30; 'code': 0.31; 'anybody': 0.32; 'turns': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'that,': 0.34; "can't": 0.34; 'changed': 0.34; 'list': 0.35; 'adds': 0.35; 'faster': 0.35; 'received:org': 0.36; 'but': 0.36; 'should': 0.36; 'does': 0.37; 'detail': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'skip:n 10': 0.63; 'behavior': 0.64; 'our': 0.65; 'smith': 0.71; 'article': 0.78; 'received:fios.verizon.net': 0.84; 'items,': 0.91; 'try.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Interesting list() un-optimization
Date Sun, 10 Mar 2013 17:39:14 -0400
References <roy-572C99.22201106032013@70-1-84-166.pools.spcsdns.net> <roy-87E0C0.09050910032013@70-1-84-166.pools.spcsdns.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3
In-Reply-To <roy-87E0C0.09050910032013@70-1-84-166.pools.spcsdns.net>
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.3167.1362951587.2939.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362951587 news.xs4all.nl 6889 [2001:888:2000:d::a6]:42796
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41038

Show key headers only | View raw


On 3/10/2013 9:05 AM, Roy Smith wrote:
> In article <roy-572C99.22201106032013@70-1-84-166.pools.spcsdns.net>,
>   Roy Smith <roy@panix.com> wrote:
>
>> 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!
>
> Hmmm, I think I've found a good solution.
>
> It turns out, we don't actually use QuerySet in our models.  We've
> defined our own QuerySet subclass which adds a few convenience methods.
> Adding
>
>      def __len__(self):
>          raise NotImplemented
>
> to our subclass should do the job.  It looks like list() respects that,
> calls __iter__(), and does the right thing.  I can't find any place
> where that behavior for list() is documented,

It is a cpython implementation detail that probably has changed with the 
versions.

> but it's logical and experimentally, it seems to work.

> Can anybody see any downside to this?

No. Give it a try.


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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