[Python-ideas] Marvelous movable memoryview
Matt Chaput
matt at whoosh.ca
Fri Jul 27 00:40:33 CEST 2012
By which I mean a memoryview that lets you change the start and end
offsets of its view of the underlying object (not modifying the
underlying object).
Let's say I want to slice a long string s into trigrams and write them
to disk (or something).
for i in xrange(0, len(s) - 3):
x = s[i:i + 3]
myfile.write(x)
At each step the slice copies the bytes of the string, even though all I
do is write them to disk.
I could avoid copying with memoryviews...
for i in xrange(0, len(s) - 3):
x = memoryview(s)[i:i + 3]
myfile.write(x)
...but this is actually much slower (3x slower in some quick tests). I'm
guessing it's because of all the object creation (while string slicing
probably uses fast paths).
Shouldn't I be able to do this?
m = memoryview(s)
for i in xrange(0, len(s) - 3):
m.start = i
m.end = i + 3
myfile.write(m)
Cheers,
Matt
More information about the Python-ideas
mailing list