Thanks for that advice, and I'll follow it up immediately with the script below, which I made after the OP and which is probably indeed bad code, although I think it does give the time discrepancies that I am looking for (qr much faster than eval). I suppose the main thing I missed all along (I had been told things in CB before), is the slowness of eval. Well, now I know :)
#!/usr/bin/perl -w use strict; use warnings; use Benchmark qw /cmpthese/; cmpthese 10_000 => { 'do_eval' => sub { do_eval() }, 'do_qr' => sub { do_qr() }, }; sub do_eval { my $genome = "AGTATCGATCGATGCATGCTAGCTAGCTAGCTAGCTAGCTAGSTGCTAGCT"; my @regexes = ('abc', '^qwerty'); # dont care my $count = 0; my $code = 'if ($genome =~ /' . join ('/ && /', @regexes) . '/) { + $count++; }'; eval $code; die "Error: $@\n Code:\n$code\n" if ($@); } sub do_qr { my $string = "AGTATCGATCGATGCATGCTAGCTAGCTAGCTAGCTAGCTAGSTGCTAGCT"; my @regexes = ("abc", "^qwerty"); # dont care my $count = 0; my @compiled = map qr/$_/, @regexes; for(my $i=0; $i<@regexes; $i++) { if($string =~ /$compiled[$i]/){ $count++; } } } __END__ Rate do_eval do_qr do_eval 2162/s -- -77% do_qr 9242/s 328% --
In reply to Re^4: eval string possibilities
by erix
in thread eval string possibilities
by erix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |