improving a huge double-for cycle

skip at pobox.com skip at pobox.com
Thu Sep 18 08:44:29 EDT 2008


    Alex> Unfortunately my len(IN) is about 100.000 and the running time
    Alex> about 15h !!!! :(

    Alex> Any idea to improve it?

numpy?

    http://numpy.scipy.org/
    http://www.scipy.org/Numpy_Example_List

More immediately, note that you are building a list of len(IN) ints every
time through the inner loop.  A quick hit might be this simple change:

    indexes = range(len(IN))
    for i in indexes: #scan all elements of the list IN
      for j in indexes:
        if i != j:
         if (IN[i].coordinates[0] == IN[j].coordinates[0] and
             IN[i].coordinates[1] == IN[j].coordinates[1]):
             SN.append(IN[i].label)

Skip



More information about the Python-list mailing list