[Python-ideas] Marvelous movable memoryview
Antoine Pitrou
solipsis at pitrou.net
Fri Jul 27 01:25:56 CEST 2012
On Thu, 26 Jul 2012 18:40:33 -0400
Matt Chaput <matt at whoosh.ca> wrote:
> 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.
To be honest, copying three bytes is a trivial operation compared to
all the rest (interpreting bytecode, instantiating and destructing
bytes objects, calling I/O routines, etc.).
> 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)
You wouldn't win anything over the simple bytes slicing approach, IMO.
(even with PyPy or another interpreter)
Regards
Antoine.
--
Software development and contracting: http://pro.pitrou.net
More information about the Python-ideas
mailing list