Dear monks
I have a code as below:
$MI_T=MI($hypo,$text,\%hash_es,\%hash_en); sub MI { my ($string_es,$string_en,$hash_es,$hash_en)=@_; my @array_es= my @array_en = (); @array_es = split ' ', $hash_es{$string_es}; @array_en = split ' ', $hash_en{$string_en}; my $prob_es = ($#array_es+1)/6939873; my $prob_en = ($#array_en+1)/6939873; my $intersection= Intersection(\@array_es,\@array_en); my $prob_es_en= ($intersection)/6939873; $prob_es_en = ($prob_es_en + ($prob_es*$prob_en*0.1))/1.1; my $mi= $prob_es_en * log( $prob_es_en / ($prob_es * $prob_en) ); return $mi; } sub Intersection { my( $refA, $refB ) = @_; my %counts; ++$counts{ $_ } for @$refA; ++$counts{ $_ } for @$refB; my $intersects = 0; $counts{ $_ } > 1 and ++$intersects for keys %counts; return $intersects; }
my hash is like this: (term is the key and the numbers are value of each term)
term1 1 3 5 8 9 15 90 term2 23 56 789 23 1 54 89 term3 23 345 677 456 23 .... termn 54 6 768 5678 34 56 78
while the number of digits corresponding to each term can vary form 1 to n.
currently it takes a long time to calculate the $MI_T.
(sometimes 20-25 seconds). is there anyway to improve the code in order to calculate it faster? Thanks!

In reply to improving the speed by perl_lover_always

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.