You should gather your lines in a buffer and write more then one line
at once.
The best way would be to add each line to a list first, concat them
with a linefeed character
and then write them to disk in one call. If memory consumption is a
issue you could write each
100 or each 1000 lines... and the clear the list again
Works like this:
buffer = []
# just an extra optimization, so we do not need to look up the
# append method of the list object on each iteration
append = buffer.append
for i in range(numberOfHairs):
for j in range (numberOfPoints):
append( "\t\t%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f%.3f"
% (
A[j][0], A[j][1], A[j][2], B[j][0]-A[j][0], B[j]
[1]-A[j][1],
B[j][2]-A[j][2], C[j], D[j][0], D[j][1], D[j]
[2])
)
f.write( "\n".join(buffer) )
f.close()
#######
Note that i took the "\n" out of the append call and instead made it
the
merge string parameter for the join() call . You could also leave it
there and call
"".join(buffer) instead. but i find it cleaner that way.
This should be considerably faster.
Cheers,
Lutz
On 15 Okt., 17:12, Horvátth Szabolcs <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm doing some data exporting using a python scripted plugin command and
> found the file output being very slow.
> In a specific case the file write took 90 seconds and when I commented
> the f.write() stuff (so the command only did the queries) it dropped to
> 15 seconds.
> This is the structure of the write part:
>
> for i in range(numberOfHairs):
> ...
> for j in range (numberOfPoints):
> ...
> f.write("\t\t%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f
> %.3f\n" % (A[j][0], A[j][1], A[j][2], B[j][0]-A[j][0], B[j][1]-A[j][1],
> B[j][2]-A[j][2], C[j], D[j][0], D[j][1], D[j][2]))
> f.close()
>
> Am I doing completely wrong? What can I do to speed up ascii file writing?
>
> Cheers,
> Szabolcs
--~--~---------~--~----~------------~-------~--~----~
Yours,
Maya-Python Club Team.
-~----------~----~----~----~------~----~------~--~---