As requested by /msg here is a comparison of the three methods so far using Benchmark. Particle's method is only 50% slower than a regex so it kicks a$$ too at more than three times faster than the range method. TIMTOWDI. For those who would like to learn a really easy cut'n'paste way to use Benchmark (like here) check out Benchmark made easy

C:\>perl test.pl Benchmark: timing 100000 iterations of Mine, Particle, Yours... Mine: 5 wallclock secs ( 4.06 usr + 0.00 sys = 4.06 CPU) @ 24 +630.54/s ( n=100000) Particle: 8 wallclock secs ( 7.20 usr + 0.00 sys = 7.20 CPU) @ 13 +888.89/s ( n=100000) Yours: 25 wallclock secs (24.67 usr + 0.00 sys = 24.67 CPU) @ 40 +53.51/s (n =100000) C:\>
use Benchmark; my $mine = <<'ME'; my $stuff = <<EOF; boring boring boring boring boring boring boring boring boring boring boring boring boring boring boring START My wonderful super duper yummy booty bag information END boring boring boring boring boring boring boring boring boring boring boring boring boring boring boring EOF my ($good_stuff) = $stuff =~ m/(START.*END)/s; # print $good_stuff; ME my $yours = <<'YOU'; my $stuff = <<EOF; boring boring boring boring boring boring boring boring boring boring boring boring boring boring boring START My wonderful super duper yummy booty bag information END boring boring boring boring boring boring boring boring boring boring boring boring boring boring boring EOF my @lines = split("\n", $stuff); my $good_stuff; foreach(@lines){ if(m/START/ .. m/END/) { $good_stuff .= "$_\n"; } } # print $good_stuff; YOU my $particle = <<'PARTICLE'; my $stuff = <<EOF; boring boring boring boring boring boring boring boring boring boring boring boring boring boring boring START My wonderful super duper yummy booty bag information END boring boring boring boring boring boring boring boring boring boring boring boring boring boring boring EOF sub find_between_tags($$$) { my $string_to_parse = shift; my $start_tag = shift; my $start_pos = index $stuff, $start_tag; return unless $start_pos > -1; my $stop_tag = shift; my $stop_pos = index $stuff, $stop_tag; $stop_pos > -1 ? return substr $stuff, $start_pos, $stop_pos - $start_pos + length $stop_tag : return substr $stuff, $start_pos, -1; } my $good_stuff = find_between_tags($stuff, "START", "END"); # print $good_stuff; PARTICLE # prove they all work, uncomment the prints # (we don't want to print when benchmarking) # uncomment these evals # eval $mine; # eval $yours; # Benchmark those suckers timethese(100000,{ 'Mine' => $mine, 'Yours' => $yours, 'Particle' => $particle });

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: Range This by tachyon
in thread Efficiently Extracting a Range of Lines (was: Range This) by skazat

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.