这是我的 oneliner 解决方案(已编辑)。
urlpath.partition("?")[0].strip("/").replace("/", ".")
正如其他一些人提到的那样,这里的速度改进可以忽略不计。除了使用 re.compile() 来预编译你的表达式,我会开始寻找其他地方。
import re
re1 = re.compile("(\?.*|\/[0-9a-f]{24})")
re2 = re.compile("\/[0-9\/]*")
re3 = re.compile("\;.*")
re4 = re.compile("\/")
re5 = re.compile("\.api")
def orig_regex(urlpath):
urlpath=re1.sub("",urlpath)
urlpath=re2.sub("/",urlpath)
urlpath=re3.sub("",urlpath)
urlpath=re4.sub(".",urlpath)
urlpath=re5.sub("api",urlpath)
return urlpath
myregex = re.compile(r"([^/]+)")
def my_regex(urlpath):
return ".".join( x.group() for x in myregex.finditer(urlpath.partition('?')[0]))
def test_nonregex(urlpath)
return urlpath.partition("?")[0].strip("/").replace("/", ".")
def test_func(func, iterations, *args, **kwargs):
for i in xrange(iterations):
func(*args, **kwargs)
if __name__ == "__main__":
import cProfile as profile
urlpath = u'/api/v4/path/apiCallTwo?host=wApp&trackId=1347158'
profile.run("test_func(orig_regex, 10000, urlpath)")
profile.run("test_func(my_regex, 10000, urlpath)")
profile.run("test_func(non_regex, 10000, urlpath)")
结果
Iterating orig_regex 10000 times
60003 function calls in 0.108 CPU seconds
....
Iterating my_regex 10000 times
130003 function calls in 0.087 CPU seconds
....
Iterating non_regex 10000 times
40003 function calls in 0.019 CPU seconds
不做 re.compile 你的 5 正则表达式结果
running <function orig_regex at 0x100532050> 10000 times
210817 function calls (210794 primitive calls) in 0.208 CPU seconds