in reply to Re^3: question about reg exp engine
in thread question about reg exp engine

Show your benchmark. I see about a 75x difference between the single regex and the multiple regex solutions offered by the OP.

Note that benchmarks are rather like statistics: Lies, damn lies and benchmarks.


Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re^5: question about reg exp engine
by broomduster (Priest) on Aug 04, 2008 at 00:11 UTC
    I have a feeling I may be about to learn something... ;-)
    use strict; use warnings; use Benchmark qw( cmpthese ); my $results = cmpthese( -10, { 'r3' => sub { my $string = " stuff "; $string =~ s/^\s//g; $string =~ s/\s$//g; $string =~ s/\s+$//g; }, 'r1' => sub { my $string = " stuff "; $string =~ s/^\s|\s$|\s+$//g; }, } );

    Rate r1 r3 r1 295926/s -- -1% r3 299980/s 1% --

    Updated: Now that I see GrandFather's detailed Benchmark below, I see that my error was to use a short string. When I change to
    my $string = (' ' x 1000) . 'x' . (' ' x 1000);
    matching GrandFather's, I get the following:
    Rate r3 r1 r3 61890/s -- -79% r1 297607/s 381% --
    So Lies, damn lies, and benchmarks (with the wrong data), indeed.

      Changing "  stuff  " to (' ' x 1000) . 'x' . (' ' x 1000) gives:

      Rate r1 r3 r1 1849/s -- -99% r3 166166/s 8888% --

      It is important that a benchmark test what you think it is testing so what you think is being tested must entail a significant portion of the benchmark's processing time.

      Update: BTW, my result for your original benchmark was:

      Rate r1 r3 r1 767220/s -- -10% r3 855450/s 11% --

      Perl reduces RSI - it saves typing
        I didn't see this reply before I Updated above, but now we're in the same direction, even if the magnitude of difference is off. As I said earlier, the largest difference I saw for the short string was 3% (about a dozen runs, looking back).

        BTW, I can confirm your observation about the penalty associated with 'nibble' vs. 'swallow' vs. both.