writing to file very slow
Skip Montanaro
skip at pobox.com
Wed Mar 26 10:08:51 EST 2003
Moritz> Everything seems to work fine, except for the fact that writing
Moritz> the file is very slow (example: 4 minutes for 4 thousand
Moritz> lines).
...
Moritz> varnames=""
Moritz> for z in range(len(results.listfields())-1):
Moritz> varnames += str(results.fieldname(z))+'|'
Moritz> varnames += str(results.fieldname(len(results.listfields())-1))
Moritz> f.write(varnames)
Moritz> f.write("\n")
Moritz> for x in range(results.ntuples()):
Moritz> var = ""
Moritz> for y in range(len(results.listfields())-1):
Moritz> var+=str(results.getresult()[x][y])+'|'
Moritz> var+=str(results.getresult()[x][len(results.listfields())-1])
Moritz> f.write(var)
Moritz> f.write('\n')
First thing I'd try is getting rid of the string extension using +=. Also,
note that you have two nested for loops which makes your writing
proportional to the length of ntuples * the length of results.listfields().
For example:
varnames = []
for z in range(len(results.listfields())-1):
varnames.extend([str(results.fieldname(z)),'|'])
varnames.append(str(results.fieldname(len(results.listfields())-1)))
f.write("".join(varnames))
I'm sure the logic can be further cleaned up, but my head is foggy this
morning with a cold. Others with a clearer head will be able to help there.
Skip
More information about the Python-list
mailing list