Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^7: Algorithm for cancelling common factors between two lists of multiplicands (192.5 ms)

by sk (Curate)
on Aug 14, 2005 at 01:03 UTC ( [id://483641]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Algorithm for cancelling common factors between two lists of multiplicands (192.5 ms)
in thread Algorithm for cancelling common factors between two lists of multiplicands

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]); }
Output

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)

Replies are listed 'Best First'.
Re^8: Algorithm for cancelling common factors between two lists of multiplicands (192.5 ms)
by tmoertel (Chaplain) on Aug 14, 2005 at 03:58 UTC
    tmoertel's haskell implementation amazes me on the precision! I wonder if that is specific to Haskell or the way it was coded
    The reason the implementation has such precision is not because it is written in Haskell – although that does make coding more convenient – but because it computes the answer exactly as a ratio of bigints and then extracts decimal digits until the desired precision has been obtained. If you want more or fewer than sixity digits, for example, just change 60 to the desired number in the sciformat 60 call in the code in Re^13: Algorithm for cancelling common factors between two lists of multiplicands.

    Cheers,
    Tom

Re^8: Algorithm for cancelling common factors between two lists of multiplicands (192.5 ms)
by BrowserUk (Patriarch) on Aug 14, 2005 at 04:31 UTC

    I think most (though not all), of your gain is through avoiding the overhead of calling subroutines in loops. Inlining is the last step when trying squeeze out the very last drop of performance. That relates back to my opinion on Perl 5's greatest limitation is...?.

    tmoertel's haskell implementation amazes me on the precision!

    The precision comes from using Haskell's infinite precision Integer data type (analogous to Math::BigInt) for the product and division calculations, once the cancelling has been carried out.

    The performance of that precision comes from the compiled implementation and a highly optimising compiler. The only reason the Perl code manages to beat the compiled Haskell is because it uses the much lower precision, FPU native double representation.

    You might find the following code interesting, it's a brute force conversion of Tom's Haskell code staying as close to the original as I could achieve. It works okay for the small dataset, but highlights two major differences between Perl and Haskell.

    The highly recusive nature of the algorithm exacerbates the cost of Perl's subcalls and lack of tail recursion.

    And if you try to use it on the larger datasets, you'll see that the Perl implementation consumes vast amounts of memory (eventually segfaulting on my machine when it runs out of swap space). Most of the damage is done in building and rebuilding zillions of lists whilst keeping copies of earlier versions on the stack in the merge & cancel functions. It's this type of recursive, divide & copy, list processing that Haskell's ubiquitous lazy operations really excel at.

    Not particularly useful given it limitations, but an interesting exercise none the less.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://483641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found