Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: substrings that consist of repeating characters

by vr (Curate)
on Sep 29, 2020 at 17:24 UTC ( [id://11122343]=note: print w/replies, xml ) Need Help??


in reply to substrings that consist of repeating characters

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found