python – 如何避免子串

python – 如何避免子串,第1张

概述我目前处理的字符串部分如下: for (i, j) in huge_list_of_indices: process(huge_text_block[i:j]) 我想避免生成这些临时子串的开销.有任何想法吗?也许是以某种方式使用索引偏移的包装器?这是我目前的瓶颈. 请注意,process()是另一个期望字符串作为输入的python模块. 编辑: 有些人怀疑是否存在问题.以下是一些示例结果: 我目前处理的字符串部分如下:
for (i,j) in huge_List_of_indices:    process(huge_text_block[i:j])

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

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

编辑:

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

import timeimport stringtext = string.letters * 1000def 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 texttimeit(test_1)timeit(test_2)timeit(test_3)

输出:

test_1 took 972.046 mstest_2 took 47.620 mstest_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_lostring

HTH!

总结

以上是内存溢出为你收集整理的python – 如何避免子串全部内容,希望文章能够帮你解决python – 如何避免子串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1207071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存