List comprehension timing difference.

Bart Kastermans bkasterm at gmail.com
Fri Sep 2 15:54:50 CEST 2011


MRAB <python at mrabarnett.plus.com> writes:

> On 02/09/2011 01:35, Bart Kastermans wrote:

>> graph = [[a,b] for a in data for b in data if d(a,b) ==1 and a<  b]

>> graph2 = []
>> for i in range (0, len(data)):
>>      for j in range(0,len(data)):
>>          if d(data[i],data[j]) == 1 and i<  j:
>>              graph2.append ([i,j])

>
> Are they actually equivalent? Does graph == graph2?
>
> The first version (list comprehension) creates a list of pairs of
> values:
>
>     [a, b]
>
> whereas the second version (for loops) creates a list of pairs of
> indexes:
>
>     [i, j]
>
> The second version has subscripting ("data[i]" and "data[j]"), which
> will slow it down.

You are absolutely right.  I had changed the code from the
equivalent:

    graph2 = []
    for i in range (0, len(data)):
        for j in range(0,len(data)):
            if d(data[i],data[j]) == 1 and i < j:
                graph2.append ([data[i],data[j]])

But then also tried the equivalent

    for a in data:
        for b in data:
            if d(a,b) == 1 and a < b:
                graph2.append([a,b])

Which does away with the indexing, and is just about exactly as
fast as the list comprehension.


That'll teach me; I almost didn't ask the question thinking it might 
be silly.  And it was, but I thought it for the wrong reason.  I tell my
students there are no stupid questions, I should listen to myself
more when I do.  Thanks!



More information about the Python-list mailing list