An important part of making benchmark code is to ensure the alternatives do the same thing. Your do_eval subroutine had a minor problem (you only had =~ $genome on the first match, instead of all of them), which I corrected. But then when I looked at the output from the two subroutines, I noticed it was different:

sub do_eval { my $genome = "AGTATCGATCGATGCATGCTAGCTAGCTAGCTAGCTAGCTAGSTGCTAGCT"; my @regexes = ('AGT', 'ATC'); # dont care my $count = 0; my $code = 'if (' . join(' && ', map { "\$genome =~ /$_/" } @regexes) . ') { $count++; }'; eval $code; die "Error: $@\n Code:\n$code\n" if ($@); return $count; # returns 1 } sub do_qr { my $string = "AGTATCGATCGATGCATGCTAGCTAGCTAGCTAGCTAGCTAGSTGCTAGCT"; my @regexes = ("AGT", "ATC"); # dont care my $count = 0; my @compiled = map qr/$_/, @regexes; for(my $i=0; $i<@regexes; $i++) { if($string =~ /$compiled[$i]/){ $count++; } } return $count; # returns 2 }

To fix that, I changed your do_eval sub to the following:

sub do_eval { my $genome = "AGTATCGATCGATGCATGCTAGCTAGCTAGCTAGCTAGCTAGSTGCTAGCT"; my @regexes = ('AGT', 'ATC'); # dont care my $count = 0; my $code = join ";", map { "\$count++ if \$genome =~ /$_/" } @regexes; eval $code; die "Error: $@\n Code:\n$code\n" if ($@); return $count; # returns 2 }

And I decided to add my own take on the matter, which generates One Big Regex, rather than a bunch of them:

sub do_genre { my $genome = "AGTATCGATCGATGCATGCTAGCTAGCTAGCTAGCTAGCTAGSTGCTAGCT"; my @regexes = ("AGT", "ATC"); # dont care my $regex = join "|", map "($_)", @regexes; my $count = () = $genome =~ /$regex/; return $count; # returns 2 }

When I run the benchmark, I get the following results:

Rate do_eval do_qr do_genre do_eval 15531/s -- -65% -90% do_qr 44671/s 188% -- -72% do_genre 157893/s 917% 253% --

Which just goes to show that the string eval is slow, but the looping is even slower. A different algorithm makes a big difference.


In reply to Re^5: eval string possibilities by revdiablo
in thread eval string possibilities by erix

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.