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


Groups > comp.lang.python > #40748

Re: Interesting list() un-optimization

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 <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.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 <wolfgang.maier@biologie.uni-freiburg.de>
Subject Re: Interesting list() un-optimization
Date Thu, 7 Mar 2013 11:22:56 +0000 (UTC)
References <roy-572C99.22201106032013@70-1-84-166.pools.spcsdns.net> <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 <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.3008.1362655396.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


Tim Chase <python.list <at> 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


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