Here is the code I used just now to test them against each other. The additional operations which I added could certainly affect the benchmark but they are the same in both cases.
use strict; use warnings; use Benchmark qw(cmpthese); my $fileName = "data1"; my $string = $ARGV[0] || die "\nNo Search String Provided.$!\n\n"; my $stringRegex = qr{$string}; my $headerRegex = qw{^Header}; ## Match Headers my $greenRegex = qr{(^Header).*)}; my $redRegex = qr{($string)}; cmpthese (1, { case1 => sub {&case1}, case2 => sub {&case2}, }, ); sub case1 { my $matches = `grep -h -F -C50 '$grepString' $fileName`; my $match; my $data; my $header; for (split /^/, $matches) { ## More efficient than array? (Scalar +s use less memory) if ($_ =~ $headerRegex) { ## Match headers if ($match) { $data =~ s/$greenRegex/\033[1;32m$1\033[m/o; ## Color + headers in GREEN $data =~ s/$redRegex/\033[1;31m$1\033[m/go; ## Color +string matches in RED $data =~ s/\n\n/\n/go; ## Remove double blanks lines print $data, "\n"; $match = 0; } $data = ""; $header = 1; } if (($header) && (0 > index ($_, $string))) { ## Match body o +f section $data .= $_; } elsif ($header) { ## Match search string $data .= $_; $match = 1; } } } sub case2 { open(my $fh, '<', "$fileName") or die $!; my $match; my $data; my $header; while (<$fh>) { if ($_ =~ $headerRegex) { if ($match) { $data =~ s/$greenRegex/\033[1;32m$1\033[m/o; $data =~ s/$redRegex/\033[1;31m$1\033[m/go; $data =~ s/\n\n/\n/go; print $data, "\n"; $match = 0; } $data = ""; $header = 1; } if (($header) && (0 > index ($_, $string))) { $data .= $_; } elsif ($header) { $data .= $_; $match = 1; } } } case1 4.79 s/iter case2 46.6 s/iter

In reply to Re^7: search array for closest lower and higher number from another array by bigbot
in thread search array for closest lower and higher number from another array by bigbot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.