ocehb: (Default)
[personal profile] ocehb
поиск 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)



PS.

DB<5> p $RE{net}{IPv4}
(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))

Date: 2006-09-26 12:00 pm (UTC)
From: [identity profile] yanychar.livejournal.com
А поиск пульта от телевизора по квартире слабО?

Date: 2006-09-26 12:27 pm (UTC)
From: [identity profile] yanychar.livejournal.com
За каждый поиск или все же за алгоритм?

Date: 2006-09-26 12:24 pm (UTC)
From: [identity profile] what-me.livejournal.com
$str = "345.2.3.4";
print A(), "\n", B();

Date: 2006-09-26 09:32 pm (UTC)
From: [identity profile] quappa.livejournal.com
timethese(-5, {A=> \&A, B => \&B})
Benchmark: running A, B for at least 5 CPU seconds...
         A:  5 wallclock secs ( 5.27 usr +  0.01 sys =  5.28 CPU) @ 208472.99/s (n=1100998)
         B:  6 wallclock secs ( 5.26 usr +  0.00 sys =  5.26 CPU) @ 33313.66/s (n=175157)

Profile

ocehb: (Default)
ocehb

January 2021

S M T W T F S
     12
345 6789
10111213141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 7th, 2026 05:19 pm
Powered by Dreamwidth Studios