例如,假设我想要一个函数对一个字符串进行转义以便在HTML语言中使用(就像在Django的escape filter中一样):
def escape(string):
"""
Returns the given string with ampersands, quotes and angle
brackets encoded.
"""
return string.replace('&', '&').replace('<', '<').replace('>', '>').replace("'", ''').replace('"', '"')这是可行的,但它很快就会变得丑陋,而且算法性能似乎很差(在本例中,字符串被重复遍历了5次)。更好的方法是这样:
def escape(string):
"""
Returns the given string with ampersands, quotes and angle
brackets encoded.
"""
# Note that ampersands must be escaped first; the rest can be escaped in
# any order.
return replace_multi(string.replace('&', '&'),
{'<': '<', '>': '>',
"'": ''', '"': '"'})这样的函数是否存在,或者是使用我之前编写的内容的标准Python习惯用法?
发布于 2010-03-21 03:17:06
您是否有一个运行速度太慢的应用程序,并且您对它进行了分析,发现像下面这样的代码片段导致了它的速度变慢?瓶颈出现在意想不到的地方。
当前代码段遍历字符串5次,每次做一件事。你建议遍历它一次,可能每次做五件事(或者至少每次做一些事情)。目前还不清楚这会不会自动给我带来更好的效果。目前使用的算法是O(n*m) (假设字符串的长度比规则中的内容长),其中n是字符串的长度,m是替换规则的数量。我认为,您可以将算法复杂度降低到O(n*log(m)),在我们所处的特定情况下-其中原始对象都只有一个字符(但通常不是在多次调用replace的情况下)-O(N),但这并不重要,因为m是5,但n是无界的。
如果m保持不变,那么两个解的复杂度实际上都是O(n)。我不清楚,尝试把五个简单的通行证变成一个复杂的通行证是不是一项有价值的任务,目前我无法猜测实际的时间。如果它有什么东西可以让它更好地扩展,我会认为这是一个更有价值的任务。
在一次传递而不是连续传递中做所有事情也需要回答关于如何处理冲突规则以及如何应用这些规则的问题。通过一系列replace,这些问题的解决方案是显而易见的。
发布于 2010-03-21 03:50:12
不如我们测试一下各种方法,看看哪种方法更快(假设我们只关心最快的方法)。
def escape1(input):
return input.replace('&', '&').replace('<', '<').replace('>', '>').replace("'", ''').replace('"', '"')
translation_table = {
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"',
}
def escape2(input):
return ''.join(translation_table.get(char, char) for char in input)
import re
_escape3_re = re.compile(r'[&<>\'"]')
def _escape3_repl(x):
s = x.group(0)
return translation_table.get(s, s)
def escape3(x):
return _escape3_re.sub(_escape3_repl, x)
def escape4(x):
return unicode(x).translate(translation_table)
test_strings = (
'Nothing in there.',
'<this is="not" a="tag" />',
'Something & Something else',
'This one is pretty long. ' * 50
)
import time
for test_i, test_string in enumerate(test_strings):
print repr(test_string)
for func in escape1, escape2, escape3, escape4:
start_time = time.time()
for i in xrange(1000):
x = func(test_string)
print '\t%s done in %.3fms' % (func.__name__, (time.time() - start_time))
print运行此命令将为您带来以下好处:
'Nothing in there.'
escape1 done in 0.002ms
escape2 done in 0.009ms
escape3 done in 0.001ms
escape4 done in 0.005ms
'<this is="not" a="tag" />'
escape1 done in 0.002ms
escape2 done in 0.012ms
escape3 done in 0.009ms
escape4 done in 0.007ms
'Something & Something else'
escape1 done in 0.002ms
escape2 done in 0.012ms
escape3 done in 0.003ms
escape4 done in 0.007ms
'This one is pretty long. <snip>'
escape1 done in 0.008ms
escape2 done in 0.386ms
escape3 done in 0.011ms
escape4 done in 0.310ms看起来一个接一个地替换它们是最快的。
编辑:以1000000次迭代再次运行测试,对前三个字符串给出以下结果(第四个字符串在我的机器上等待时间太长,我无法等待=P):
'Nothing in there.'
escape1 done in 0.001ms
escape2 done in 0.008ms
escape3 done in 0.002ms
escape4 done in 0.005ms
'<this is="not" a="tag" />'
escape1 done in 0.002ms
escape2 done in 0.011ms
escape3 done in 0.009ms
escape4 done in 0.007ms
'Something & Something else'
escape1 done in 0.002ms
escape2 done in 0.011ms
escape3 done in 0.003ms
escape4 done in 0.007ms这些数字几乎是一样的。在第一种情况下,它们实际上更加一致,因为直接字符串替换现在是最快的。
发布于 2010-03-21 02:40:45
我更喜欢干净的东西,比如:
substitutions = [
('<', '<'),
('>', '>'),
...]
for search, replacement in substitutions:
string = string.replace(search, replacement)https://stackoverflow.com/questions/2484156
复制相似问题