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) #### # 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=(); 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