in reply to Iterating over string

Only a marginal speed improvement (5-20%, compared to the respective given/when rewrite of your original version), but more of a memory usage optimization, i.e. not splitting the string into an extra array of letters, which consumes a lot more memory than the original string  (only relevant if the strings could be huge, though — in which case @A, @C etc. would be huge as well, of course...):

use 5.010; for (my $i=0; $i<length($string); $i++) { my $letter = substr($string, $i, 1); given ($letter) { when ("A") { $A[$i]++; } when ("C") { $C[$i]++; } when ("G") { $G[$i]++; } when ("T") { $T[$i]++; } default { $N[$i]++; } } }

P.S.: Perl 5.10's given/when is more than ten times as fast as the old (and generally frowned upon) switch/case.

Update:

#!/usr/local/bin/perl use 5.010; use Switch; use Benchmark 'cmpthese'; $string = "AGCTAGCTAGAAGTCGGTGACTGfoobar"; sub orig_switchcase { my @seq = split(//,$string); my $i = 0; for my $letter (@seq) { switch ($letter) { case "A" { $A0[$i]++; } case "C" { $C0[$i]++; } case "G" { $G0[$i]++; } case "T" { $T0[$i]++; } else { $N0[$i]++; } } $i++; } } sub orig_givenwhen { my @seq = split(//,$string); my $i = 0; for my $letter (@seq) { given ($letter) { when ("A") { $A1[$i]++; } when ("C") { $C1[$i]++; } when ("G") { $G1[$i]++; } when ("T") { $T1[$i]++; } default { $N1[$i]++; } } $i++; } } sub substr_givenwhen { for (my $i=0; $i<length($string); $i++) { my $letter = substr($string, $i, 1); given ($letter) { when ("A") { $A2[$i]++; } when ("C") { $C2[$i]++; } when ("G") { $G2[$i]++; } when ("T") { $T2[$i]++; } default { $N2[$i]++; } } } } sub BUK { my $string2 = $string; $string2 =~ tr[ACGT][N]c; $c{ substr $string2, $_, 1 }[ $_ ]++ for 0 .. length( $string2 )-1 +; } cmpthese(-1, { 'orig_s/c' => \&orig_switchcase, 'orig_g/w' => \&orig_givenwhen, 'substr_g/w' => \&substr_givenwhen, BUK => \&BUK, }, ); __END__ Rate orig_s/c orig_g/w substr_g/w BUK orig_s/c 2112/s -- -91% -93% -97% orig_g/w 24660/s 1067% -- -19% -68% substr_g/w 30632/s 1350% 24% -- -60% BUK 77422/s 3565% 214% 153% --