speedy Python strings?

Jp Calderone exarkun at intarweb.us
Tue Jan 20 05:29:24 CET 2004


On Mon, Jan 19, 2004 at 10:53:47PM -0500, Francis Avila wrote:
> Stuart D. Gathman wrote in message ...
> >#         text = struct.unpack("L", self.buffer[:4])
> >#         self.buffer = self.buffer[4:]
> >   pos = self.pos
> >   text = struct.unpack("L", self.buffer[pos:pos+4])
> >   self.pos = pos + 4
> 
> 
> In this vein, I would also recommend looking at the array module.  You
> didn't describe the data structure, but if each record is simply a list of
> 4-byte integers, you could convert the whole record to ints at once like so:
> 
> record = array.array('L', stringorlist)
> Then, perform your loops on record, which will be a list-like object
> supporting item insertion and deletion, and conversion to bytestrings and
> other Python base types.
> 

  Also note the existence of fromfile() - the array module actually makes it
possible to do this incredibly efficiently:

    a = array.array('L')
    a.fromfile(openFileObj, count)

  Jp




More information about the Python-list mailing list