Efficient storage of floats

Francois Petitjean littlejohn.75 at free.fr
Mon Dec 24 14:30:09 CET 2001


I have to read a number of points coordinates (npoints). Different
structures can be used to store these numbers:
1. points as a list of (x,y,z) tuples
points = []
and each time a point is read
points.append( (x,y,z) )
To use : x,y,z = points[kpt]  # very straightforward

A variant (1.b) can be to initialize the list
points = [ (0.0, 0.0, 0.0) * npoints ] or [ (0.0, 0.0, 0.0 ) ] * npoints
I don't remember the syntax.(should be the second one).

There is some overhead in using a list for storing the same type of tuples.

2. The coordinates can be stored in an array
acoords = array.array('f')
and the variant:
2.b acoords = array.array('f', [ (0.0,) *(3*npoints) ])
In the 2.a case :
acoords.append(x)
acoords.append(y)
acoords.append(z)
can be used to store a point.
or simpler (2.a2) : acoords.extend( [x,y,z] )
Similarly :
2.b1  acoords[3*kpt] = x
         acoords[3*kpt+1] = y
         acoords[3*kpt+2] = z
or
2.b2  acoords[3*kpt:3*kpt+3] = [x,y,z] # ??

Using acoords : x,y,z = acoords[3*kpt],acoords[3*kpt+1],acoords[3*kpt+2]
or simpler  x,y,z = acoords[3*kpt:3*kpt+3]

The type of contained objects (a simple float) is supposed to be stored (and
queried, checked) only one time for each evaluation of a expression with
many acoords[index] terms.

Is it possible to have a quantitative assessment of the overhead of the two
methods 1(list) or 2(array)? (overhead in memory footprint or CPU cycles).
And is it interesting to preinitialize the structure? The number of points
is typically 800*2 (for symmetry) that is say up to 8000 floats.

Regards





More information about the Python-list mailing list