我目前处理的字符串部分如下:
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!