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)
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.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)
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |