Re: [Python-Dev] bytes & bytearray

2015年1月20日 08:54:17 -0800

On Tue, Jan 20, 2015 at 11:48:10AM +0200, Paul Sokolovsky wrote:
> Hello,
> 
> On 2015年1月20日 18:15:02 +1300
> Greg Ewing <[email protected]> wrote:
> 
> > Guido van Rossum wrote:
> > > On Mon, Jan 19, 2015 at 11:43 AM, Paul Sokolovsky
> > > <[email protected] <mailto:[email protected]>> wrote:
> > > 
> > > b.lower_inplace()
> > > b.lower_i()
> > > 
> > > Please don't go there. The use cases are too rare.
> > 
> > And if you have such a use case, it's not too
> > hard to do
> > 
> > b[:] = b.lower()
> 
> The point of inplace operations (memoryview's, other stuff already in
> Python) is to avoid unneeded memory allocation and copying. For 1Tb
> bytearray with 1Tb of RAM, it will be very hard to do. (Ditto for 100K
> bytearray with 150K RAM.)
You can just loop through the bytearray and assign elements. I use
something along the lines of this for PyParallel where I'm operating on
bytearrays that are backed by underlying socket buffers, where I don't
want to do any memory allocations/reallocations:
 def toupper_bytes(data):
 assert isinstance(data, bytearray)
 a = ord('a')
 z = ord('z')
 for i in range(0, len(data)):
 c = data[i]
 if c >= a and c <= z:
 data[i] = c - 32
Low overhead, mostly stays within the same ceval frame. Should be a
walk in the park for PyPy, Cython or Numba to optimize, too.
 Trent.
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to