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

In reply to Re: substrings that consist of repeating characters by vr
in thread substrings that consist of repeating characters by Anonymous Monk

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.