in reply to Re^2: Question about speeding a regexp count
in thread Question about speeding a regexp count

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