I just compared my "a1" algorithem with the slowesr ;-) so far for 6_500_000 characters:

the unpack code took:988 wallclock secs (102.42 usr + 27.64 sys = 130.06 CPU)
the file code took:30 wallclock secs ( 9.70 usr + 0.24 sys = 9.94 CPU)

I didn't put the shorter counts in yet because, they would add neglectable time when we create them afterwards. It's at most 3*3*3*2+2=56 additions compared to 6.5 Million increments...
#!/usr/bin/perl use strict; use warnings; use Benchmark; use Data::Dumper; my $DNAlength= 6_500_000; my $sequencelength= 3; my $testfilename= 'DNA.tst'; my $bufsize= 1024; # create testdata open(my $testfile, ">$testfilename") or die "$testfilename: $!"; my $buffer = ''; for (my $i=$DNAlength; $i; --$i) { $buffer.= qw(A C G T)[rand 4]; if (length($buffer) > $bufsize) { print $testfile $buffer; $buffer = ''; } } print $testfile $buffer; close $testfile; a0: { my $a0_0=new Benchmark; open($testfile, $testfilename) or die "$testfilename: $!"; my $allbuffered= <$testfile>; close $testfile; my %count; my $template = ('AXA2X2A3X2' x (length($allbuffered) - 2)) . ' +AXA2XA'; $count{$_}++ foreach unpack $template, $allbuffered; my $a0_n=new Benchmark; print "the unpack code took:",timestr(timediff($a0_n, $a0_0)), +"\n"; } a1: { # Start collecting data my %DNA_sequence_count; my $a1_0=new Benchmark; open($testfile, $testfilename) or die "$testfilename: $!"; my $kept=0; while (1) { my $read_bytes = read($testfile, $buffer, $bufsize-$ke +pt); last unless $read_bytes; my $end_buffer = $read_bytes-$sequencelength; for (my $i=0; $i <= $end_buffer; ++$i) { ++$DNA_sequence_count{substr($buffer, $i, $seq +uencelength)}; } $kept= $sequencelength-1; $buffer= substr($buffer, $end_buffer+1, $kept); } close $testfile; my $a1_n=new Benchmark; print "the file code took:",timestr(timediff($a1_n, $a1_0)),"\ +n"; }

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

In reply to Re^3: Question about speeding a regexp count by Skeeve
in thread Question about speeding a regexp count by Commander Salamander

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.