[Tutor] Fastest way to write/read numerical data into/from a file?

Karthikesh Raju karthik at james.hut.fi
Wed Jun 30 12:16:40 EDT 2004


Hi All,

i have written a module that saves matrices in a file. Basically, a
matrix as
A = numarray.array([1,2]) will be saved as 1 2 in the file, and
A = numarray.array([[1,2],[3,4]]) as
1 2
3 4

i.e each row is a line and there are spaces between each column.
Presntly, when writing i do the following:


try:
   i,j = data.shape
   for ii in range(0,i):
       for jj in range(0,j):
              value = "%s" %data[ii,jj]
              file.write(value)
              file.write(' ')
       file.write('\n')
except ValueError:
    i = data.shape
    for ii in range(0,i):
        value = "%s" %data[ii]
        file.write(value)
        file.write(' ')
    file.write('\n')

Here, i am doing an element by element access, how can i speed this up?

While reading, i do the following:

while(line):
    temp = []
    list = string.split(line)
    for e in list: temp.append(string.atof(e))
    x = numarray.concatenate(x,numarray.array(temp))
    line = open1.readline()
open1.close()

So, while reading, i read a complete line, which i guess is faster,
any suggestions on improving the speed esp when i am writing to the file.

With warm regards

karthik


-----------------------------------------------------------------------
Karthikesh Raju,		    email: karthik at james.hut.fi
Researcher,			    http://www.cis.hut.fi/karthik
Helsinki University of Technology,  Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc.,    Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------




More information about the Tutor mailing list