Permalink
Browse files

fasta.perl: made it 2.5 times faster w/o changing the algo

  • Loading branch information...
1 parent 71aa4ec commit d7bfdf54e8c934ea1c0511d82be62b4099d1eaed Reini Urban committed Jul 3, 2011
Showing with 24 additions and 26 deletions.
  1. +24 −26 bench/fasta/fasta.perl
View
@@ -4,13 +4,13 @@
# contributed by David Pyke
# tweaked by Danny Sauer
# Butchered by Jesse Millikan
+# 2.5 times faster by Reini Urban
use constant IM => 139968;
use constant IA => 3877;
use constant IC => 29573;
-use constant LINELENGTH => 60;
-
+my $LINELENGTH = 60;
my $LAST = 42;
sub makeCumulative {
@@ -26,27 +26,25 @@ sub makeRandomFasta {
my($id,$desc,$n,$genelist) = @_;
print ">$id $desc\n";
- my $pick, $r;
-
- while($n > 0){
- $pick='';
-
- # Get LINELENGTH chars or what's left of $n
- CHAR: foreach (1 .. ($n > LINELENGTH ? LINELENGTH : $n)){
- $rand = ($LAST = ($LAST * IA + IC) % IM) / IM;
-
- # Select gene and append it
- foreach (@$genelist){
- if($rand < $_->[1]){
- $pick .= $_->[0];
- next CHAR;
- }
- }
- }
-
- print "$pick\n";
- $n -= LINELENGTH;
+ my $rand;
+ my $pick= ' ' x 4096;
+ $pick = '';
+ while($n > 0){
+ # Get LINELENGTH chars or what's left of $n
+ foreach (1 .. ($n > $LINELENGTH ? $LINELENGTH : $n)){
+ $rand = ($LAST = ($LAST * IA + IC) % IM) / IM;
+ # Select gene and append it. $genelist(char,probs) is sorted by probs
+ foreach (@$genelist){
+ if($rand < $_->[1]){
+ $pick .= $_->[0];
+ last;
+ }
+ }
+ }
+ $pick .= "\n";
+ $n -= $LINELENGTH;
}
+ print $pick;
}
# Print $n characters of $s (repeated if nessary) with newlines every LINELENGTH
@@ -55,13 +53,13 @@ sub makeRepeatFasta {
print ">$id $desc\n";
- my $ss;
+ my $ss = '';
while($n > 0){
# Overfill $ss with $s
- $ss .= $s while length $ss < LINELENGTH;
+ $ss .= $s while length $ss < $LINELENGTH;
# Print LINELENGTH chars or whatever's left of $n
- print substr($ss,0,$n > LINELENGTH ? LINELENGTH : $n,""), "\n";
- $n -= LINELENGTH;
+ print substr($ss,0,$n > $LINELENGTH ? $LINELENGTH : $n,""), "\n";
+ $n -= $LINELENGTH;
}
}

2 comments on commit d7bfdf5

Owner

rurban replied Jul 5, 2011

Note that fasta5.pl https://alioth.debian.org/tracker/index.php?func=detail&aid=313164&group_id=30402&atid=413100 by Rodrigo de Oliveira is much faster (~7x) than this version. fasta5.pl uses the better python algo.

It seems that they finally added fasta5.pl to the list, and it broke the charts!

Please sign in to comment.