Re: Optimisation Hints (dict processing and strings)

2005-03-29 Thread OPQ
#For the record, I'm not on premature optimisation anymore. 
#The code is working. I just want to save hours of computing, without
relying to much on C extensions.
#Nevertheless, thansk for tips, clarifications and explanations.

> 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
> 
> - use cStringIO instead
> - or append all chars to a list and do "".join (listvar)


Yes, but as Peter said using a list won't be useful for a single
operation.
And I have to compute the string at each step of the loop. I think
listvar won't be useful.
But I'm going to try cStringIO. 

> 
> > 
> > for (2):
> > for k in hash.keys()[:]: # Note : Their may be a lot of keys here
> >if len(hash[k])<2:
> >   del hash[k]
> 
> - Try if it isn't faster to iterate using items instead of iterating 
> over keys

items are huge lists of numbers. keys are simple small strings. And
even if it is faster, how can I find the key back, in order to delete
it ?
for v in hashh.items():
if len(v)<2:
   del ???



> - use the dict.iter* methods to prevent building a list in memory. You 
> shouldn't use these values directly to delete the entry as this could 
> break the iterator:
> 
> for key in [k for (k, v) in hash.iteritems () if len (v) < 2]:
>  del hash (key)
> 

I gonna try, but think that would be overkill: a whole list has to be
computed !
Maybe whith genexps .. for key in (k for (k,v) in hash.iteritems()
if len(v)<2)


> This of course builds a list of keys to delete, which could also be large.
> 

Yes. Which tell me to use genexps.

> - also: hash.keys()[:] is not necessary, hash.keys () is already a copy
> 

Yes I got that one, but too late !

Thanks !
-- 
http://mail.python.org/mailman/listinfo/python-list


Optimisation Hints (dict processing and strings)

2005-03-29 Thread OPQ
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
-- 
http://mail.python.org/mailman/listinfo/python-list