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


Groups > comp.lang.python > #37949

Re: looping versus comprehension

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'append': 0.07; 'subject:skip:c 10': 0.07; 'missed': 0.09; 'python': 0.09; '[0,': 0.09; '[1,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'cc:addr :python-list': 0.10; '2.7': 0.13; '2],': 0.16; '3],': 0.16; '[2,': 0.16; 'blocks': 0.16; 'looping': 0.16; 'naive': 0.16; 'operation,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:looping': 0.16; 'subject:versus': 0.16; 'wrongly': 0.16; 'wrote:': 0.17; 'drawing': 0.17; 'thu,': 0.17; 'jan': 0.18; '31,': 0.22; 'tuples': 0.22; 'cc:2**0': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; 'wonder': 0.27; 'header:X-Complaints-To:1': 0.28; 'chris': 0.28; '>>>>': 0.29; 'appending': 0.29; 'checking.': 0.29; 'coded': 0.29; 'convert': 0.29; 'points': 0.29; 'that.': 0.30; 'code': 0.31; 'point': 0.31; 'room': 0.32; 'to:addr:python-list': 0.33; 'version': 0.34; 'list': 0.35; 'clear': 0.35; 'fail': 0.35; 'lists.': 0.35; 'doing': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'test': 0.36; 'does': 0.37; 'why': 0.37; 'ones': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'worth': 0.63; 'different': 0.63; 'making': 0.64; 'teach': 0.69; 'received:109': 0.74; '2013': 0.84; 'different.': 0.84; 'experiment': 0.84; 'off,': 0.84; 'played': 0.84; 'presumably': 0.84; 'sfxlen:4': 0.84; 'timings': 0.84; 'faster.': 0.91; 'pfxlen:big': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robin Becker <robin@reportlab.com>
Subject Re: looping versus comprehension
Date Wed, 30 Jan 2013 17:58:27 +0000
References <51093511.9050701@chamonix.reportlab.co.uk> <CAPTjJmpGfNChNd6dDYZ9ncvmwbeAaF7b6fK6f_YL4rzkZtPS-g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
X-Gmane-NNTP-Posting-Host 109.174.168.73
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
In-Reply-To <CAPTjJmpGfNChNd6dDYZ9ncvmwbeAaF7b6fK6f_YL4rzkZtPS-g@mail.gmail.com>
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.1220.1359568731.2939.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359568731 news.xs4all.nl 6937 [2001:888:2000:d::a6]:48313
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37949

Show key headers only | View raw


On 30/01/2013 15:49, Chris Angelico wrote:
> On Thu, Jan 31, 2013 at 1:58 AM, Robin Becker <robin@reportlab.com> wrote:
>> however, when I tried an experiment in python 2.7 using the script below I
>> find that the looping algorithms perform better. A naive loop using list +=
>> list would appear to be an O(n**2) operation, but python seems to be doing
>> better than that. Also why does the append version fail so dismally. Is my
>> test coded wrongly or is pre-allocation of the list making this better than
>> expected?
>
> First off, are you aware that your first three blocks of code and your
> last four produce different results? The first ones flatten the list,
> the others simply convert tuples to lists. With n = 3:
>
>>>> points = []
>>>> for xy in row:
>      points += [xy[0],xy[1]]
>>>> points
> [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
>>>> map(list,row)
> [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
>
> Once that's sorted out, then timings can be played with. But it's
> worth noting that list appending is not going to be O(N*N), because
> it's going to allow room for expansion.
>
> ChrisA
>

No I missed that :( the list is a flattened one.

That'll teach me not to copy the code from the users without checking. Now you 
point it out it's clear that his code is doing something different. Presumably 
it's not drawing the same thing at all :) no wonder it got much faster.
-- 
Robin Becker

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


Thread

Re: looping versus comprehension Robin Becker <robin@reportlab.com> - 2013-01-30 17:58 +0000

csiph-web