in reply to Extract potentially quoted words

Hrm. You might consider using [^"]* and [^']* instead of .*?.

Just a thought. ;-)

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Replies are listed 'Best First'.
Re: (bbfu) (dot star) Re: Extract potentially quoted words
by merlyn (Sage) on Jun 07, 2001 at 04:02 UTC
    I might consider it, but what would the point be? Both walk the minimal chars to get to the result. Perhaps there'll be an ever-so-slight speed improvement. Now, if it had been .* instead of .*?, I'd see that.

    -- Randal L. Schwartz, Perl hacker

      I would say that using a negated character class is more efficient than using minimal matching:
      Rate minimal_c neg_class_c minimal neg_class minimal_c 94887/s -- -11% -40% -44% neg_class_c 106974/s 13% -- -32% -37% minimal 157452/s 66% 47% -- -8% neg_class 170558/s 80% 59% 8% --
      minimal_c and neg_class_c use capturing parentheses; minimal and neg_class don't. Either way there's a small but noticeable advantage for the negated character class.

      The reason for this isn't too hard to figure. With a negated character-class, the regex engine does almost no backtracking. The first thing it tries to match is [^"]+, and it succeeds every time until it finds the ".

      With minimal matching, however, the engine backtracks after every character. The first thing it tries to match is ", and it fails every time, then backs up and tries matching .+?, until it finds the ". It's doing more work that way, so it's slower.

      #!perl -w use strict; use Benchmark; Benchmark->import(qw/cmpthese/) if $^V; my $time = shift || 10; my $len = shift || 1000; my $abc = 'abc' x $len; my $str = '$abc"$abc"$abc'; my %bms = ( minimal => sub { $str =~ /".*?"/ }, neg_class => sub { $str =~ /"[^\"]*"/ }, minimal_c => sub { $str =~ /"(.*?)"/ }, neg_class_c => sub { $str =~ /"([^\"]*)"/ }, ); if ($^V) { cmpthese(-$time, \%bms); } else { timethese(-$time, \%bms); }
        With minimal matching, however, the engine backtracks after every character. The first thing it tries to match is ", and it fails every time, then backs up and tries matching .+?, until it finds the ". It's doing more work that way, so it's slower.
        No, perhaps you are confusing .+ with .+?. With .+?, it's inching forward a character at a time each time it can't find a " immediately there.

        -- Randal L. Schwartz, Perl hacker

      *shrug* The latter is more efficient (if only by a little, as you point out) and (to me, anyway) a little more clear, conceptually. And there's no real reason not to, besides personal preference. My preference is, why make the RE do more than it needs to? :-)

      Anyway, it was just a thought. It is a useful snippet, btw. :-)

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

      Negated Character Classes include the newline. Dot doesn't.