Printing to file, how do I do it efficiently?

Cameron Walsh cameron.walsh at gmail.com
Fri Nov 10 01:12:16 EST 2006


Hi all,

I have a numpy.array of 89x512x512 uint8's, set up with code like this:


data=numpy.array([],dtype="uint8")
data.resize((89,512,512))
# Data filled in about 4 seconds from 89 image slices
<snip lots of processing code>


I first tried writing this data to a binary raw format (for use in a
program called Drishti) as follows:

# The slow bit:
volumeFile=file("/tmp/test.raw","wb")
for z in xrange(Z):
    for y in xrange(Y):
        for x in xrange(X):
            volumeFile.write("%c" %(data[z,y,x]))
volumeFile.close()

That took about 39 seconds.

My second attempt was as follows:
volumeFile = open("/tmp/test2.raw","wb")
data.resize((X*Y*Z)) # Flatten the array
for i in data:
    volumeFile.write("%c" %i)
data.resize((Z,Y,X))
volumeFile.close()

This took 32 seconds.  (For those of you unfamiliar with numpy, the
data.resize() operations take negligible amounts of time, all it does is
allow the data to be accessed differently.)

I'm guessing that the slow part is the fact that I am converting the
data to character format and writing it one character at a time.  What
is a better way of doing this, or where should I look to find a better way?


Thanks,

Cameron.



More information about the Python-list mailing list