Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Here is my implementation and results with benchmark. It is 45-50% faster. I am not sure what it the exact digit at which the result becomes unreliable so it could be slightly off

My perl skills are not that great so my implementation is very simple and straightforward

tmoertel's haskell implementation amazes me on the precision! I wonder if that is specific to Haskell or the way it was coded

My implementation

#!/usr/bin/perl use strict; use warnings; use Benchmark::Timer; my $timer= new Benchmark::Timer; my $tag = "fet inputs: " . join (',',@ARGV); $timer->start($tag); my (@snum,@sden,@nmul,@dmul) = (); my ($i,$p,$power) = (0,0,0); construct (@ARGV); @snum = sort {$b<=>$a} @snum; @sden = sort {$b<=>$a} @sden; # Make the arrays equal size if (@snum < @sden) { foreach $i (@snum..$#sden) { $snum[$i] = 0;} } else { foreach $i (@sden..$#snum) { $sden[$i] = 0;} } for $i (0..$#snum) { my @elements; if ($snum[$i] > $sden[$i]) { @elements = (($sden[$i]+1)..$snum[$i]); my ($num,$np) = multiply(\@elements); push(@nmul,$num); $power += $np; } elsif ($sden[$i] > $snum[$i]) { @elements = (($snum[$i]+1)..$sden[$i]); my ($den,$dp) = multiply(\@elements); push(@dmul,$den); $power -= $dp; } } my ($num,$den) = (1,1); $num *= $_ for (@nmul); $den *= $_ for (@dmul); printf ("Ratio = %.17fe%d\n", $num/$den,$power); $timer->stop($tag); print $timer->report; sub multiply { my $p = 0; my $x = shift; my $product = 1; for (@$x) { $product *= $_; while ($product > 1) { $product /= 10; $p++; } while ($product < 0.1) { $product *= 10; $p--; } } return ($product,$p); } sub construct { my @data = @_; my $R1 = $data[0]+$data[1]; my $R2 = $data[2]+$data[3]; my $C1 = $data[0]+$data[2]; my $C2 = $data[1]+$data[3]; my $N = $R1 + $R2; @snum = ($R1,$R2,$C1,$C2); @sden = ($N,$data[0],$data[1],$data[2],$data[3]); }

I gave enough time between each run to avoid any CPU heating/fan issues

program BUK = your implementation to avoid machine speed variation.

C:\>perl fet 989 9400 43300 2400 Ratio = 0.08070604647867725e-7028 1 trial of fet inputs: 989,9400,43300,2400 (80.804ms total) C:\>perl fet 1e5 2e5 2e5 1e5 Ratio = 0.13249945964968005e-14759 1 trial of fet inputs: 1e5,2e5,2e5,1e5 (3.289s total) C:\>perl fet 1e6 2e6 2e6 1e6 Ratio = 0.02733537591856761e-147574 1 trial of fet inputs: 1e6,2e6,2e6,1e6 (38.062s total) C:\>perl buk 989 9400 43300 240 0.80706046478677251e-7029 1 trial of [989 9400 43300 2400] (163.696ms total) C:\>perl buk 1e5 2e5 2e5 1e5 1.32499459649680040e-14760 1 trial of [1e5 2e5 2e5 1e5] (6.552s total) C:\>perl buk 1e6 2e6 2e6 1e6 2.73353759185676100e-147576 1 trial of [1e6 2e6 2e6 1e6] (70.739s total)

In reply to Re^7: Algorithm for cancelling common factors between two lists of multiplicands (192.5 ms) by sk
in thread Algorithm for cancelling common factors between two lists of multiplicands by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2020-02-18 09:23 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (75 votes). Check out past polls.