regexp vs. regexp+code
Sep. 26th, 2006 02:42 pmпоиск ip в тексте
( регулярное выражение )
# perl -MRegexp::Common=net -MBenchmark=timethese -de0
DB<1> $str = "a 192.168.1.1 test 192.168.300.1 testii 127.0.0.1 c"
DB<2> sub A () { return $str =~ m|($RE{net}{IPv4})|go; }
DB<3> sub B () { my @ret; while ($str =~ m|(\d+\.\d+\.\d+\.\d+)|g) { \
unless (grep { $_ > 255 } split /\./,$1) { push @ret,$1; } } \
return @ret; }
DB<4> timethese (10_000,{'A' => \&A,'B' => \&B})
Benchmark: timing 10000 iterations of A, B...
A: 36 wallclock secs (34.11 usr + 0.36 sys = 34.47 CPU) @ 290.11/s (n=10000)
B: 1 wallclock secs ( 0.48 usr + 0.00 sys = 0.48 CPU) @ 20833.33/s (n=10000)
( регулярное выражение )