improving a huge double-for cycle

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Sep 19 01:20:20 CEST 2008


Bruno Desthuilliers:
> def doubles9():
> ...
>      SN = []
>      sn_append = SN.append

Few more:

import psyco

def doubles10():
     dup = set()
     SN = []
     for item in IN:
         c = item.coordinates
         if c in dup:
             SN.append(item)
         else:
             dup.add(c)
     return SN

psyco.bind(doubles10)


from collections import deque

def doubles11():
     dup = set()
     dup_add = dup.add
     SN = deque()
     sn_append = SN.append

     for item in IN:
         c = item.coordinates
         if c in dup:
             sn_append(item)
         else:
             dup_add(c)
     return SN


def doubles12():
     dup = set()
     SN = deque()
     for item in IN:
         c = item.coordinates
         if c in dup:
             SN.append(item)
         else:
             dup.add(c)
     return SN

psyco.bind(doubles12)


Timings:

doubles00 : 0.522365288653
doubles01 : 0.247219812198
doubles02 : 0.237889823898
doubles03 : 0.238638921389
doubles04 : 0.23821698217
doubles05 : 0.177042495425
doubles06 : 0.13166199162
doubles08 : 0.00569725197252
doubles09 : 0.00418566685667
doubles10 : 0.00192086920869
doubles11 : 0.00403324533245
doubles12 : 0.00184026840268

Hopefully that's fast enough :-)

Bye,
bearophile



More information about the Python-list mailing list