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% --

In reply to Re: Iterating over string by almut
in thread Iterating over string by danielfortin86

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.