避免在Python中进行不必要的切片复制 [英] Avoiding unnecessary slice copying in Python

查看:103
本文介绍了避免在Python中进行不必要的切片复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这样的情况下,有没有避免无意义的切片复制的惯用语:

Is there a common idiom for avoiding pointless slice copying for cases like this:

>>> a = bytearray(b'hello')
>>> b = bytearray(b'goodbye, cruel world.')
>>> a.extend(b[14:20])
>>> a
bytearray(b'hello world')

在我看来,创建b[14:20]切片时发生不必要的复制.我不是说要在内存中创建一个新的切片以提供给extend,而是要说仅使用当前对象的这一范围".

It seems to me that there is an unnecessary copy happening when the b[14:20] slice is created. Rather than create a new slice in memory to give to extend I want to say "use only this range of the current object".

某些方法可以帮助您使用切片参数,例如count:

Some methods will help you out with slice parameters, for example count:

>>> a = bytearray(1000000)       # a million zero bytes
>>> a[0:900000].count(b'\x00')   # expensive temporary slice
900000
>>> a.count(b'\x00', 0, 900000)  # helpful start and end parameters
900000

但是很多,例如我的第一个示例中的extend,都没有此功能.

but many, like extend in my first example, don't have this feature.

我意识到,对于许多应用程序,我所谈论的将是微优化,因此在有人问之前-是的,我已经对应用程序进行了概要分析,这是我的情况值得担心的地方.

I realise that for many applications what I'm talking about would be a micro-optimisation, so before anyone asks - yes, I have profiled my application, and it is something worth worrying about for my case.

我在下面有一个解决方案",但是最好的主意是欢迎的.

I have one 'solution' below, but any better ideas are most welcome.

推荐答案

创建buffer对象可以避免复制切片,但是对于较短的切片,仅进行复制会更有效:

Creating a buffer object avoids copying the slice, but for short slices it's more efficient to just make the copy:

>>> a.extend(buffer(b, 14, 6))
>>> a
bytearray(b'hello world')

这里只有一个内存副本,但是创建buffer对象的成本远远超过了节省的成本.不过,对于较大的切片,它应该更好.我不确定要使此方法总体上更有效,必须切成薄片.

Here there's only one copy made of the memory, but the cost of creating the buffer object more than obliterates the saving. It should be better for larger slices though. I'm not sure how large the slice would have to be for this method to be more efficient overall.

请注意,对于Python 3(以及可选的python 2.7),您需要一个memoryview对象:

Note that for Python 3 (and optionally in Python 2.7) you'd need a memoryview object instead:

>>> a.extend(memoryview(b)[14:20])

这篇关于避免在Python中进行不必要的切片复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆