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


Groups > comp.lang.python > #37949 > unrolled thread

Re: looping versus comprehension

Started byRobin Becker <robin@reportlab.com>
First post2013-01-30 17:58 +0000
Last post2013-01-30 17:58 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#37949 — Re: looping versus comprehension

FromRobin Becker <robin@reportlab.com>
Date2013-01-30 17:58 +0000
SubjectRe: looping versus comprehension
Message-ID<mailman.1220.1359568731.2939.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web