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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |