And here is my results (I used the same test.txt as my post above):#!/usr/bin/perl use Benchmark; #get start and end comment my $start = $ARGV[0]; my $end = $ARGV[1] || "\n"; my $file; open(FILE, "test.txt") || die; { local $/ = undef; #set to 'slurp' mode $file = <FILE>; #read entire file into $_ } close FILE; timethese(100000, { 'parse1' => sub { &parse1($file) }, 'parse2' => sub { &parse2($file) }, }); sub parse1 { my $file = shift; while( $file =~ /\Q$start\E(.*?)\Q$end\E/sg ) { $a = $1; $match = $&; #look for more start tags in what we matched while( $a =~ /\Q$start\E/sg ) { #balance the ending comments $file =~ /.*?\Q$end\E/sg; $match .= $&; } #print $match, "\n"; } return $match; } sub parse2 { my $file = shift; my $inside = 0; my $oldpos = 0; while ($file =~ /(\Q$start\E|\Q$end\E)/g) { if ($1 eq $start) { if (++$inside == 1) { $oldpos = pos($file) - length($start); } } else { if (--$inside == 0) { return substr($file, $oldpos, pos($file)-$oldpos); } } } }
prompt% parse.pl '/*' '*/' Benchmark: timing 100000 iterations of parse1, parse2... parse1: 42 wallclock secs (30.07 usr + 0.11 sys = 30.18 CPU) parse2: 17 wallclock secs (13.60 usr + 0.04 sys = 13.64 CPU)
In reply to Re: matching comments
by perlmonkey
in thread matching comments
by Eugene
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |