preallocate list

J

Jim

John said:
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

Thanks for looking John. For that I should take a little time to explain.

I tried to rename the variables, some of them were four words long. I
got a couple of the renames wrong. Sorry.

Regarding 'works'. I meant that with a text file of four lines the code
completed. With my desired size 11000 lines it didn't complete within
the limits of my patience. I didn't try any other size.

Also I perhaps wrongly use the newsgroup threads paradigm in trying to
restart my query with extra information (that turned out a little faulty).

Luckily the other branches yielded fruit.

Thanks again
Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top