in reply to Re^4: Pattern match array
in thread Pattern match array
use strict; use warnings; use Benchmark q{cmpthese}; my @prims = qw{ int char long double static }; my $inFile = q{xxx.c}; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my $outFile = q{/dev/null}; open my $outFH, q{>}, $outFile or die qq{open: $outFile: $!\n}; cmpthese( -10, { JohnGG => sub { seek $inFH, 0, 0; my $rxPrims = do { local $" = q{|}; qr{\b(@prims)\b}; }; while ( <$inFH> ) { next unless my @found = m{$rxPrims}g; print $outFH qq{Found @found on line $.\n}; } }, Narveson => sub { seek $inFH, 0, 0; while ( <$inFH> ) { for my $word ( m{\b(\w+)\b}g ) { if (grep {$word eq $_} @prims) { print $outFH qq{Found $word on line $.\n}; } } } }, Narveson2 => sub { seek $inFH, 0, 0; my %sought = map { $_ => 1 } @prims; while ( <$inFH> ) { for my $word ( m{\b(\w+)\b}g ) { if ( $sought{$word} ) { print $outFH qq{Found $word on line $.\n}; } } } }, } ); close $inFH or die qq{close: $inFile: $!\n}; close $outFH or die qq{close: $outFile: $!\n};
Rate Narveson Narveson2 JohnGG Narveson 1.39/s -- -30% -64% Narveson2 1.99/s 43% -- -48% JohnGG 3.81/s 174% 91% --
Cheers,
JohnGG
|
|---|