Here's a second round of benchmarking. I've added two things -- the suggested removal of object creation from the loop and a straight-forward regex with string studying. Here is the code, with the benchmarks at the bottom this time. :) My interpretation is that there isn't an appreciable difference caused by the object instantiation, nor by studying. I suspect the slight difference in run-time are more likely just the result of increased server load while the benchmark script was running.

use strict; use Bio::SeqIO; use Benchmark; use Regexp::Assemble; use Carp; my $seqio = Bio::SeqIO->new( -file => "chrY.fa.masked", -format => "fa +sta" ); my $seq = $seqio->next_seq(); my $sequence = $seq->seq(); my @strings = ( 'CACGTG', 'GTGCAC' ); my $t0 = new Benchmark; for (1..100) { foreach my $string (@strings) { my $len = length($string); while ($sequence =~ /$string/g) { my $val = pos($seque +nce) - $len; } } } my $t1 = new Benchmark; my $t2 = new Benchmark; for (1..100) { foreach my $string (@strings) { my $pos = 0; my $len = length($string); while ( ($pos = index($sequence, $string, $pos)) >= 0 +) { my $val = substr($sequence, $pos, $len); $pos += $len; } } } my $t3 = new Benchmark; my $t4 = new Benchmark; for (1..100) { my $ra = Regexp::Assemble->new(); $ra->add(@strings); while ( $sequence =~ /$ra/g) { my $val = pos($sequence) - 6; } } my $t5 = new Benchmark; my $t6 = new Benchmark; my $ra = Regexp::Assemble->new(); $ra->add(@strings); for (1..100) { while ( $sequence =~ /$ra/g) { my $val = pos($sequence) - 6; } } my $t7 = new Benchmark; my $t8 = new Benchmark; for (1..100) { study($sequence); foreach my $string (@strings) { my $len = length($string); while ($sequence =~ /$string/g) { my $val = pos($seque +nce) - $len; } } } my $t9 = new Benchmark; my $td1 = timediff($t1, $t0); my $td2 = timediff($t3, $t2); my $td3 = timediff($t5, $t4); my $td4 = timediff($t7, $t6); my $td5 = timediff($t9, $t8); print 'Regexp : ', timestr($td1), "\n"; print 'Index : ', timestr($td2), "\n"; print 'Merged object inside loop : ', timestr($td3), "\n"; print 'Merged object out of loop : ', timestr($td4), "\n"; print 'Regexp with studying : ', timestr($td5), "\n";

And the results:

Regexp : 221 wallclock secs (221.08 usr + 0.05 sys + = 221.13 CPU) Index : 22 wallclock secs (21.49 usr + 0.00 sys = + 21.49 CPU) Merged object inside loop : 636 wallclock secs (636.61 usr + 0.21 sys + = 636.82 CPU) Merged object out of loop : 651 wallclock secs (651.06 usr + 0.18 sys + = 651.24 CPU) Regexp with studying : 240 wallclock secs (239.50 usr + 0.27 sys + = 239.77 CPU)

In reply to Re: Multiple Regex's on a Big Sequence by bernanke01
in thread Multiple Regex's on a Big Sequence by bernanke01

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.