preallocate list

Jim jbo at cannedham.ee.ed.ac.uk
Wed Apr 13 11:34:37 EDT 2005


Steven Bethard wrote:
> Jim wrote:
> 
>> Thanks for the suggestions. I guess I must ensure that this is my 
>> bottle neck.
>> <code>
>>     def readFactorsIntoList(self,filename,numberLoads):
>>     factors = []
>>         f = open(self.basedir + filename,'r')
>>         line = f.readline()
>>         tokens = line.split()
>>         columns = len(tokens)
>>         if int(columns) == number:
>>             for line in f:
>>                 factor = []
>>                 tokens = line.split()
>>                 for i in tokens:
>>                     factor.append(float(i))
>>                 factors.append(loadFactor)
>>         else:
>>             for line  in f:
>>                 tokens = line.split()
>>                 factors.append([float(tokens[0])] * number)
>>     return factors
>> </code>
>>
>> OK. I've just tried with 4 lines and the code works. With 11000 lines 
>> it uses all CPU for at least 30 secs. There must be a better way.
> 
> 
> Was your test on *just* this function?  Or were you doing something with 
> the list produced by this function as well?
> 

Just this. I had a breakpoint on the return.

I'm going to try peufeu's line of code and I'll report back.

Jim



More information about the Python-list mailing list