http://qs321.pair.com?node_id=1205957


in reply to Performance penalty of using qr//

Sorry, your benchmark is too complicated, I can't easily tell how often the inner loop is executed.

If it's just several times then your results may be explained by the overhead to initialize qr.

Another guess is to try $s =~ /$re/; instead of $s =~ $re; (which is admittedly counterintuitive)

Can't test, sorry.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Performance penalty of using qr//
by Athanasius (Archbishop) on Dec 21, 2017 at 06:44 UTC

    Hi LanX,

    The inner for loops are each executed 3,789,182 times.

    And yes, I originally had $s =~ /$re/ instead of $s =~ $re — it made no appreciable difference.

    Update: Deparsing suggests that Perl adds the slashes anyway:

    16:57 >perl -we "my $q = qr/^(\d+)$/; 123 =~ $q; print qq[$1\n];" 123 16:57 >perl -MO=Deparse -we "my $q = qr/^(\d+)$/; 123 =~ $q; print qq[ +$1\n];" BEGIN { $^W = 1; } my $q = qr/^(\d+)$/; 123 =~ /$q/; print "$1\n"; -e syntax OK 16:57 >

    Cheers,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      hmm .... could be a regression.

      If for some reason the scalar falls back to string interpolation instead of using the compilated code this could explain the penalty and wouldn't break the tests.

      A simplified benchmark for all platforms x versions would be nice.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Wikisyntax for the Monastery