加入收藏 | 设为首页 | 会员中心 | 我要投稿 大庆站长网 (https://www.0459zz.com/)- 科技、智能边缘云、事件网格、云计算、站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何避免子串

发布时间:2020-12-30 20:48:10 所属栏目:Python 来源:互联网
导读:我目前处理的字符串部分如下: for (i, j) in huge_list_of_indices: process(huge_text_block[i:j]) 我想避免生成这些临时子串的开销.有任何想法吗?也许是以某种方式使用索引偏移的包装器?这是我目前的瓶颈. 请注意,process()是另一个期望字符串作为输入的

我目前处理的字符串部分如下:

for (i,j) in huge_list_of_indices:
    process(huge_text_block[i:j])

我想避免生成这些临时子串的开销.有任何想法吗?也许是以某种方式使用索引偏移的包装器?这是我目前的瓶颈.

请注意,process()是另一个期望字符串作为输入的python模块.

编辑:

有些人怀疑是否存在问题.以下是一些示例结果:

import time
import string
text = string.letters * 1000

def timeit(fn):
    t1 = time.time()
    for i in range(len(text)):
        fn(i)
    t2 = time.time()
    print '%s took %0.3f ms' % (fn.func_name,(t2-t1) * 1000)

def test_1(i):
    return text[i:]

def test_2(i):
    return text[:]

def test_3(i):
    return text

timeit(test_1)
timeit(test_2)
timeit(test_3)

输出:

test_1 took 972.046 ms
test_2 took 47.620 ms
test_3 took 43.457 ms

解决方法

我想你要找的是 buffers.

缓冲区的特征是它们“切片”支持缓冲区接口的对象而不复制其内容,但基本上在切片的对象内容上打开“窗口”.一些更多的技术解释可用于here.摘录:

Python objects implemented in C can export a group of functions called the “buffer interface.” These functions can be used by an object to expose its data in a raw,byte-oriented format. Clients of the object can use the buffer interface to access the object data directly,without needing to copy it first.

在您的情况下,代码应该看起来或多或少像这样:

>>> s = 'Hugely_long_string_not_to_be_copied'
>>> ij = [(0,3),(6,9),(12,18)]
>>> for i,j in ij:
...     print buffer(s,i,j-i)  # Should become process(...)
Hug
_lo
string

HTH!

(编辑:大庆站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读