シンプルなテキストパーサーの2つのバージョンがあります(ログインの正確性を検証します):
rgx = re.compile(r"^[a-zA-Z][a-zA-Z0-9.-]{0,18}[a-zA-Z0-9]$")
def rchecker(login):
return bool(rgx.match(login))
max_len = 20
def occhecker(login):
length_counter = max_len
for c in login:
o = ord(c)
if length_counter == max_len:
if not (o > 96 and o < 123) and \
not (o > 64 and o < 91): return False
if length_counter == 0: return False
# not a digit
# not a uppercase letter
# not a downcase letter
# not a minus or dot
if not (o > 47 and o < 58) and \
not (o > 96 and o < 123) and \
not (o > 64 and o < 91) and \
o != 45 and o != 46: return False
length_counter -= 1
if length_counter < max_len:
o = ord(c)
if not (o > 47 and o < 58) and \
not (o > 96 and o < 123) and \
not (o > 64 and o < 91): return False
else: return True
else: return False
correct_end = string.ascii_letters + string.digits
correct_symbols = correct_end + "-."
def cchecker(login):
length_counter = max_len
for c in login:
if length_counter == max_len and c not in string.ascii_letters:
return False
if length_counter == 0:
return False
if c not in correct_symbols:
return False
length_counter -= 1
if length_counter < max_len and c in correct_end:
return True
else:
return False
すべて同じ動作をする 3 つの方法があります。ログインのいくつかのルールを確認してください。正規表現ルールで明らかだと思います。280000 回のログインでこれらのメソッドの cProfile ベンチマークを作成したところ、理解できない結果が得られました。
正規表現で
560001 function calls in 1.202 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
280000 0.680 0.000 1.202 0.000 logineffcheck.py:10(rchecker)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
280000 0.522 0.000 0.522 0.000 {method 'match' of '_sre.SRE_Pattern' objects}
順序付き
3450737 function calls in 8.599 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
280000 5.802 0.000 8.599 0.000 logineffcheck.py:14(occhecker)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
3170736 2.797 0.000 2.797 0.000 {ord}
メソッドで
280001 function calls in 1.709 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
280000 1.709 0.000 1.709 0.000 logineffcheck.py:52(cchecker)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
正しい形式で 10 万のログイン、キリル文字で 6 万のログイン、20 ではなく 24 の長さの 6 万のログイン、長さ 0 の 6 万のログインを作成しました。つまり、28 万あります。正規表現が ord を使用した単純なサイクルよりもはるかに高速であることをどのように説明しますか?