Monks,

I have an efficiency question. I need to create a super large tab delimited file. 4^7 by 4^7 in dimension. What I have now is a double loop which prints out each element individually in its appropriate location. This file is a symmetric square matrix. I am unsure whether it will take more time to save 0.5 *4^7 *4^7 entries to RAM so that I only calculate once, or whether it would be better to calculate each element twice inside the double loop. If any of the monks are especially bored, I would also appreciate comments on how I could improve the efficiency of the code below. Please forgive me if there are glaring errors, I am not a programmer, just a lowly grad student pretending to be one.

#!/usr/bin/perl use strict; use warnings; my @kmers=<>; chomp @kmers; my $kmer_l=length($kmers[0]); for(my $j=0;$j<@kmers;++$j){ print &map_constraint($kmer_l,&return_constraint($kmers[$j],$kmers +[0])); for(my $i=1;$i<@kmers;++$i){ print "\t",&map_constraint($kmer_l,&return_constraint($kmers[$ +j],$kmers[$i])); } print "\n"; } sub map_constraint{ my $dt=0.0000000001; ##input string length,and edit distance value return (-(($_[1]+$dt)/(($_[0]/2)+$dt)-1)) ##returns a value from ( +-1,1) } sub return_constraint{#string edit distance my ($len1, $len2) = (length $_[0], length $_[1]); if ($len1 == 0){ return $len2; } if ($len2 == 0){ return $len1; } my %hash; for(my $i=0;$i<=$len1;++$i){ for(my $j=0;$j<=$len2;++$j){ $hash{$i}{$j} = 0; $hash{0}{$j} = $j; } $hash{$i}{0}=$i; } my @a=split(//, $_[0]); my @b=split(//, $_[1]); for(my $i=1;$i<=$len1;++$i){ for (my $j=1;$j<=$len2;++$j){ my $cost=($a[$i-1] eq $b[$j-1]) ? 0 : 1; $hash{$i}{$j}=&min([$hash{$i-1}{$j} + 1,$hash{$i}{$j-1} + +1,$hash{$i-1}{$j-1} + $cost]); } } return $hash{$len1}{$len2}; } sub min{ my $min=${$_[0]}[0]; foreach my $elem (@{$_[0]}){ if($elem < $min){ $min=$elem; } } return $min;

In reply to Efficiency of implementation by azheid

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.