in reply to how can I speed up this perl??

Short of going to a completely different hash implementation, there are incremental improvements you can make to improve your code... You can factor out the indexing operations: Instead of
if ( ($genome[$i] eq 'a') && ($genome[$i+1] eq 'a')) { ++$tt; } elsif ( ($genome[$i] eq 'a') && ($genome[$i+1] eq 'g')) { ++$ag; } elsif ( ($genome[$i] eq 'a') && ($genome[$i+1] eq 'c')) { ++$ac; } elsif ( ($genome[$i] eq 'a') && ($genome[$i+1] eq 't')) { ++$at; } elsif ( ($genome[$i] eq 't') && ($genome[$i+1] eq 'a')) { ++$ta; } ...
use...
my $genome = $genome[$i]; my $genome1 = $genome[$i+1]; if ( ($genome eq 'a') && ($genome1 eq 'a')) { ++$tt; } elsif ( ($genome eq 'a') && ($genome1 eq 'g')) { ++$ag; } elsif ( ($genome eq 'a') && ($genome1 eq 'c')) { ++$ac; } elsif ( ($genome eq 'a') && ($genome1 eq 't')) { ++$at; } elsif ( ($genome eq 't') && ($genome1 eq 'a')) { ++$ta; } ...
Finding the array elements is done only once, rather than once per comparison. The next improvement would be to concatenate the two elements and halve the number of comparisons try...
my $genomepair = $genome[$i] . $genome[$i+1]; if ( $genomepair eq 'aa' ) { ++$tt; } elsif ( $genomepair eq 'ag') { ++$ag; } elsif ( $genomepair eq 'ac') { ++$ac; } elsif ( $genomepair eq 'at') { ++$at; } elsif ( $genomepair eq 'ta') { ++$ta; } ...

Replies are listed 'Best First'.
Re: A less extreme change..
by dragonchild (Archbishop) on Nov 24, 2003 at 14:44 UTC
    Immediately, what seems to be a bug comes out.
    if ( $genomepair eq 'aa' ) { ++$tt; }

    If it's 'aa', why increment $tt? If it's not a bug, then it needs a comment explaining why it doesn't follow the pattern.

    Continuing with the refactoring, once you have it in the second form listed, you can easily move to a hash.

    my %pair_count; my $genomepair = $genome[$i] . $genome[$i+1]; $pair_count{$genomepair}++;

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.