The task at hand shouts "RLE!!!" at me. General purpose RLE, efficiently (let's hope so) implemented (i.e. coded in C), accessed from Perl -- why, PDL, of course.
The benchmark below is probably very skewed because my test DNA consists of only short same base (nucleotide) fragments. Let's assume the ultimate goal is length of longest "C's" string and its position. The only other contestant is salva's code, modified to fit stated purpose. Sorry if I missed faster other monks' solution.
Note: sneaking Perl's scalar as PDL raw data looks hackish, which it is. Opening scalar as filehandle and then using readflex to stuff PDL raw data is, alas, too slow.
use strict;
use warnings;
use Time::HiRes 'time';
use Readonly;
Readonly my $SIZE => 10_000_000;
my $str;
{ # get us some data
use String::Random 'random_regex';
srand 1234;
$str = random_regex( "[ACTG]{$SIZE}" );
}
{
print "\nlet's test PDL!\n";
use PDL;
my $t = time;
my $p = PDL-> new_from_specification( byte, $SIZE );
${ $p-> get_dataref } = $str;
$p-> upd_data;
my ( $lengths, $values ) = $p-> rle;
my $cumu = $lengths-> cumusumover;
my $C_lengths = $lengths * ( $values == ord 'C' );
my ( undef, $max, undef, $max_ind ) = $C_lengths-> minmaximum;
report( $max, $cumu-> at( $max_ind - 1 ), time - $t )
}
{
print "\nlet's test pure Perl's re-engine!\n";
my $t = time;
my $best = [ -1, -1 ];
while ( $str =~ /((C)\2+)/g ) {
$best = [ length( $1 ), $-[ 1 ]]
if length $1 > $best-> [ 0 ]
}
report( @$best, time - $t )
}
sub report {
printf "\tmax run of C's is %d bases long at %d\n\ttime consumed:
+%f\n", @_
}
__END__
let's test PDL!
max run of C's is 11 bases long at 4367281
time consumed: 0.164513
let's test pure Perl's re-engine!
max run of C's is 11 bases long at 4367281
time consumed: 0.361907
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.