Created
December 19, 2013 03:54
-
-
Save sharl/8034168 to your computer and use it in GitHub Desktop.
python regex is too slow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# aa.py | |
from __future__ import print_function | |
import sys | |
import time | |
import re | |
def inner0(n): | |
for i in range(n): | |
r = re.compile('@sharlm') | |
r.search('x' * 100) | |
def outer1(n): | |
r = re.compile('@sharlm') | |
for i in range(n): | |
r.search('x' * 100) | |
def outer2(n): | |
r = re.compile('@sharlm') | |
line = 'x' * 100 | |
for i in range(n): | |
r.search(line) | |
def notre1(n): | |
for i in range(n): | |
try: | |
('x' * 100).index('@sharlm') | |
except ValueError: | |
pass | |
def notre2(n): | |
line = 'x' * 100 | |
for i in range(n): | |
try: | |
line.index('@sharlm') | |
except ValueError: | |
pass | |
def lap(func, n): | |
start = time.time() | |
func(n) | |
end = time.time() | |
print(str(func), end - start) | |
def main(): | |
funcs = [ | |
inner0, | |
outer1, | |
outer2, | |
notre1, | |
notre2, | |
] | |
n = 1000000 | |
for f in funcs: | |
lap(f, n) | |
if __name__ == '__main__': | |
main() | |
# aa.pl | |
use Time::HiRes qw(gettimeofday tv_interval); | |
sub inner0 { | |
my $n = shift; | |
for (1 .. $n) { | |
("x" x 100) =~ m/\@sharlm/o; | |
} | |
} | |
sub outer1 { | |
my $n = shift; | |
my $r = qr/\@sharlm/o; | |
for (1 .. $n) { | |
("x" x 100) =~ $r; | |
} | |
} | |
sub outer2 { | |
my $n = shift; | |
my $r = qr/\@sharlm/o; | |
my $line = "x" x 100; | |
for (1 .. $n) { | |
$line =~ $r; | |
} | |
} | |
sub lap { | |
my ($func, $n) = @_; | |
my $start = [gettimeofday]; | |
$func->($n); | |
my $end = [gettimeofday]; | |
printf "$func %f\n", tv_interval($start, $end); | |
} | |
sub main { | |
my @funcs = ( | |
\&inner0, | |
\&outer1, | |
\&outer2, | |
); | |
my $n = 1000000; | |
foreach my $f (@funcs) { | |
lap($f, $n); | |
} | |
} | |
main(); | |
$ python aa.py | |
<function inner0 at 0x107086aa0> 2.08626699448 | |
<function outer1 at 0x107092410> 0.664885997772 | |
<function outer2 at 0x10709b488> 0.542963027954 | |
<function notre1 at 0x10709b500> 1.68027710915 | |
<function notre2 at 0x10709b578> 1.57071399689 | |
$ perl aa.pl | |
CODE(0x7fd35b803678) 0.169853 | |
CODE(0x7fd35b817a60) 0.579516 | |
CODE(0x7fd35b080c90) 0.607407 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh...