python – 如何避免子串
我目前处理的字符串部分如下: 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.摘录:
在您的情况下,代码应该看起来或多或少像这样: >>> 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! (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Python:TypeError:*之后的参数必须是一个序列
- python – 随机裁剪数据增强卷积神经网络
- 一个基于python的PowerShell?
- python – 在模板中显示存储为二进制blob的图像
- python – 用于打开具有两个函数的文件的“with”
- python unicode rendering:如何知道字体中是否缺
- Python绘图:如何使matplotlib.pyplot停止强制我
- python – 在SQLAlchemy中动态设置__tablename__
- python – 如何在PyQtWebkit中为QNetworkRequest
- Python functools.namedtuple