哪个正则表达式更有效?

时间:2011-09-14 22:01:52

标签: python regex

s/(?P<head>\[\[foo[^\[]*)abc/\g<head>def

s/(?=\[\[foo[^\[]*)abc/def

哪个效率更高?还有其他方法可以提高效率吗?请注意,尽管我使用Perl风格的语法进行说明,但实际上我使用的是Python的re库,它不允许使用\K(keep)关键字。

1 个答案:

答案 0 :(得分:6)

使用(?P<head>\[\[foo[^\[]*)abc模块的python中的

re更快:

import time
import re

rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')

total1, total2 = 0.0, 0.0

def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)

for x in xrange(50000):
    total1 += timeRE(1)

for x in xrange(50000):
    total2 += timeRE(2)

print total1
print total2

输出:

4.27380466461
16.9591507912

编辑(在同一循环中再次运行两次):

for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)

输出:

4.26199269295
17.2384319305

编辑(修复子以匹配问题):

import time
import re
rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')
total1, total2 = 0.0, 0.0
def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("\g<head>def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)

for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)
print total1
print total2

输出:

Run 1:
4.62282061577
17.8212277889

Run 2:    
4.6660721302
17.1630160809

Run 3:
4.62124109268
17.21393013

编辑(使用与REGEX匹配的字符串):

import time
import re

rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')
total1, total2 = 0.0, 0.0

def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_<head>_<tail>_</head>_</tail>_abcdefghijklmnopqrstuvwxyz_<head>[[fooBAR_ABCDEFGHIJKLMNOPQRSTUVWXYZ_abc]]]]defghiojklmnopqrstuvwyz" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("\g<head>def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)

for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)

print total1
print total2

输出:

23.4271130562
29.6934807301

最后一次:

import time
import re
rec1 = re.compile('(?P<head>\[\[foo[^\[]*)abc')
rec2 = re.compile('(?=\[\[foo[^\[]*)abc')
total1, total2 = 0.0, 0.0
def timeRE(ver):
    x = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890_<head>_<tail>_</head>_</tail>_abcdefghijklmnopqrstuvwxyz_<head>[[fooBAR_ABCDEFGHIJKLMNOPQRSTUVWXYZ_abc]]]]defghiojklmnopqrstuvwyz" * 100)
    t1 = time.time()
    if ver is 1:
        rec1.sub("\g<head>def", x)
    else:
        rec2.sub("def", x)
    return (time.time() - t1)
for x in xrange(50000):
    total1 += timeRE(1)
    total2 += timeRE(2)
print "Method 1: Avg run took: %+0.7f - With a total of: %+0.7f" % ((total1 / 50000.0), total1)
print "Method 2: Avg run took: %+0.7f - With a total of: %+0.7f" % ((total2 / 50000.0), total2)

输出:

Method 1: Avg run took: +0.0004924 - With a total of: +24.6196477
Method 2: Avg run took: +0.0005921 - With a total of: +29.6053855