preallocate list

John Machin sjmachin at lexicon.net
Wed Apr 13 16:33:05 EDT 2005


On Wed, 13 Apr 2005 14:28:51 +0100, Jim <jbo at cannedham.ee.ed.ac.uk>
wrote:

>Thanks for the suggestions. I guess I must ensure that this is my bottle 
>neck.
><code>
>     def readFactorsIntoList(self,filename,numberLoads):

1. "numberLoads" is not used. 

>	factors = []
>         f = open(self.basedir + filename,'r')
>         line = f.readline()
>         tokens = line.split()
>         columns = len(tokens)
>         if int(columns) == number:

2. "columns" is already an int (unless of course you've redefined
"len"!). Doing int(columns) is pointless.
3. What is "number"? Same as "numberLoads"?
4. Please explain in general what is the layout of your file and in
particular, what is the significance of the first line of the file and
of the above "if" test. 

>             for line in f:
>                 factor = []
>                 tokens = line.split()
>                 for i in tokens:
>                     factor.append(float(i))

4. "factor" is built and then not used any more??

>                 factors.append(loadFactor)

5. What is "loadFactor"? Same as "factor"?

>         else:
>             for line  in f:
>                 tokens = line.split()
>                 factors.append([float(tokens[0])] * number)

6. You throw away any tokens in the line after the first??

>	return factors
></code>
>
>OK. I've just tried with 4 lines and the code works.

Which code works? The code you posted? Please define "works".


> With 11000 lines it 
>uses all CPU for at least 30 secs. There must be a better way.

Perhaps after you post the code that you've actually run, and
explained what your file layout is, and what you are trying to
achieve, then we can give you some meaningful help.

Cheers,

John






More information about the Python-list mailing list