Optimisation Hints (dict processing and strings)

OPQ poyol at hotmail.com
Tue Mar 29 09:34:46 EST 2005


Hi all,


I'd happy to have you share some thougts about ultimate optimisations
on those 2 topics:

(1)- adding one caractere at the end of a string (may be long)
(2)- in a dict mapping a key to a list of int, remove every entrie
where the list of int have of length < 2


So far, my attempts are
for (1): 
>>>longone=longone + char # where len(char)== 1
I known that string concatenation is time consuming, but a small test
on timeit seems to show that packing and creating an array for those 2
elements is equally time consuming


for (2):
for k in hash.keys()[:]: # Note : Their may be a lot of keys here
   if len(hash[k])<2:
      del hash[k]


Here again, I think the hash.keys duplication can be time *and* memory
consuming. But still better than (I suppose - no time it yet)
hash=dict([(k,v) for (k,v) in hash if len(v)>1])


I know that I can experiment further with timeit, but still would like
to hear from your experience.

Thanks ! 

--OPQ



More information about the Python-list mailing list