I must say I have been puzzled too..... I have now run a test with an adaption of the short code I posted and got these results (I'll stick my test code at the bottom, its rough as a badgers arse)

root@tivpre-master:/home/robinp # ./regexps with eval "optimisation" timethis 5000: 56 wallclock secs (52.42 usr + 0.00 sys = 52.42 CPU) naive timethis 5000: 16 wallclock secs (15.63 usr + 0.00 sys = 15.63 CPU)
however the other results I posted were for real life code where I do get a magnitude better performance using the eval method. Unfortunately I have written the code for a client and can post neither it or the regex I am using (perhaps I can sanitise a couple of example regex). I suspect that the more complex regex I use and the large number make the saving in re-compilation overcome the cost of running eval. For simpler cases and less regex doing an eval probably costs more than the recompiling overhead.

Cheers,
R.

# foul dirty test code #!/usr/local/bin/perl -w use strict; use integer; use Benchmark; my ($i, $compile_me, @names)=(1, "{my \@matches;", "no match"); my $count=5000; my @DATA=(<DATA>); foreach (@DATA) { next if /^\s*$/; last if /END CONFIG/; chomp; my ($name, $reg)=split; push @names, $name; $compile_me.="push \@matches, $i if /$reg/;"; $i++; } $compile_me.="\@matches}"; print "with eval\n"; timethis ($count, \&with_eval); sub with_eval { my ($data, $niks); foreach (@DATA) { next unless $data or (/END CONFIG/ and $data++); next if /^\s*$/; chomp; # print "\nmatches found for $_\n"; my @matches=eval $compile_me; foreach (@matches) { # print $names[$_] , "\n" # we would do something here $niks=time; } } } my (@regex); @names=(); foreach (@DATA) { next if /^\s*$/; last if /END CONFIG/; chomp; my ($name, $reg)=split; push @names, $name; push @regex, $reg; } print "naive\n"; timethis ($count, \&naive); sub naive { my ($data, $niks); foreach my $line (@DATA) { next unless $data or ($line=~/END CONFIG/ and $data++); next if $line=~/^\s*$/; chomp $line; $i=0; # print "\nmatches found for $line\n"; foreach (@regex) { # we would do something here # print $names[$i] , "\n" if $line=~/$_/; $niks=time if $line=~/$_/; $i++; } } } __DATA__ Fred_and_Friends fr.d Paul_and_co paul some_numbers \d{2} freud_likes_fred fr complex \d+.{2}\s*[r-x]+Thing\S END CONFIG freud fred NaNa pauline 12312sdfsdf 2 34 xrstThingFlaFla asdfasdf asdgfasdfg erwtwertert wertewr t asdf asdf sd fsd fasdh werhwearfuisa '[puf 768a\r4iluf7283 4 werter twertewr terwtewrtwer er woriqy rt 3245 44ugr qg5 t53q4iu 3q4a5 3qtr54 asdfo sdf sdf7-8afawbefagf we r9 yeffawe dfhoa;ef8347r 94y3rgfawer89 7 ewr7 8yawe t

In reply to Re^6: Matching against list of patterns by Random_Walk
in thread Matching against list of patterns by Eyck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.