Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: looping versus comprehension

Reply
Thread Tools

Re: looping versus comprehension

 
 
Chris Angelico
Guest
Posts: n/a
 
      01-30-2013
On Thu, Jan 31, 2013 at 1:58 AM, Robin Becker <> 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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-31-2013
On Thu, 31 Jan 2013 02:49:31 +1100, Chris Angelico wrote:

> it's worth
> noting that list appending is not going to be O(N*N), because it's going
> to allow room for expansion.


This is true for list.append, which is amortized constant time. But it is
not true for list addition, alist + blist, which is O(N**2) and hence
gets really, really slow:

steve@runes:~$ python -m timeit "L = []" "for i in xrange(1000): L = L + [1]"
100 loops, best of 3: 3.08 msec per loop
steve@runes:~$ python -m timeit "L = []" "for i in xrange(5000): L = L + [1]"
10 loops, best of 3: 71 msec per loop
steve@runes:~$ python -m timeit "L = []" "for i in xrange(25000): L = L + [1]"
10 loops, best of 3: 2.06 sec per loop


Notice that as the number of list additions goes up by a factor of 5,
the time taken goes up by a factor of 25.

--
Steven
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      01-31-2013
In article <5109fe6b$0$11104$>,
Steven D'Aprano <steve+> wrote:

> On Thu, 31 Jan 2013 02:49:31 +1100, Chris Angelico wrote:
>
> > it's worth
> > noting that list appending is not going to be O(N*N), because it's going
> > to allow room for expansion.

>
> This is true for list.append, which is amortized constant time. But it is
> not true for list addition, alist + blist, which is O(N**2) and hence
> gets really, really slow:
>
> steve@runes:~$ python -m timeit "L = []" "for i in xrange(1000): L = L + [1]"
> 100 loops, best of 3: 3.08 msec per loop
> steve@runes:~$ python -m timeit "L = []" "for i in xrange(5000): L = L + [1]"
> 10 loops, best of 3: 71 msec per loop
> steve@runes:~$ python -m timeit "L = []" "for i in xrange(25000): L = L + [1]"
> 10 loops, best of 3: 2.06 sec per loop
>
>
> Notice that as the number of list additions goes up by a factor of 5,
> the time taken goes up by a factor of 25.


It's not the addition, per-se, that's the problem. It's the creation of
a new list each time. If you use +=, it's back to O(n):

~$ python -m timeit "L = []" "for i in xrange(1000): L += [1]"
1000 loops, best of 3: 275 usec per loop

~$ python -m timeit "L = []" "for i in xrange(5000): L += [1]"
1000 loops, best of 3: 1.34 msec per loop

~$ python -m timeit "L = []" "for i in xrange(25000): L += [1]"
100 loops, best of 3: 6.91 msec per loop
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: looping versus comprehension Robin Becker Python 0 01-30-2013 05:58 PM
looping versus comprehension Robin Becker Python 0 01-30-2013 02:58 PM
List comprehension in if clause of another list comprehension Vedran Furac( Python 4 12-19-2008 01:35 PM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57