import re,timeit
str = "abcdefghigkqpwueriwutghlskajdf,mnbz,xcnvoiequynf12341234321065498+7mnbz,xcnvoiequynf12341234321065498+7mnbz,xcnvoiequynf12341234321065498+7mnbz,xcnvoiequynf12341234321065498+7"
# pattern1
start = timeit.default_timer()
for i in range(1000000):
tst = re.sub("mn|123|[abcdef7]", "", str)
stop = timeit.default_timer()
print('pattern1', 'Time: ', stop - start)
# pattern2
start = timeit.default_timer()
for i in range(1000000):
tst = str.replace("mn", "")
tst = tst.replace("123", "")
tst = tst.replace("a", "")
tst = tst.replace("b", "")
tst = tst.replace("c", "")
tst = tst.replace("d", "")
tst = tst.replace("e", "")
tst = tst.replace("f", "")
tst = tst.replace("7", "")
stop = timeit.default_timer()
print('pattern2', 'Time: ', stop - start)
# pattern3
start = timeit.default_timer()
for i in range(1000000):
tst = re.sub("mn", "", str)
stop = timeit.default_timer()
print('pattern3', 'Time: ', stop - start)
# pattern4
start = timeit.default_timer()
for i in range(1000000):
tst = str.replace("mn", "")
stop = timeit.default_timer()
print('pattern4', 'Time: ', stop - start)
# 输出
# pattern1 Time: 10.851562640495892
# pattern2 Time: 4.726493571613192
# pattern3 Time: 1.5144934460814898
# pattern4 Time: 0.5535754144044809
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.