http://qs1969.pair.com?node_id=483658


in reply to Re^7: 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

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.

#! perl -slw use strict; $| = 1; package FET; our @EXPORT = 'pCutoff'; use List::Util qw[ sum reduce ]; sub toRatio; sub rpCutoff; sub rdivide; sub fac; sub rtimes; sub cance +l; sub merge; sub pCutoff; sub pCutoff{ toRatio rpCutoff @_ } sub rpCutoff { my @rs = map{ sum @$_ } @_; my @cs = map{ sum @$_ } [ $_[0][0], $_[1][0] ], [ $_[0][1], $_[1][ +1] ]; my $n = sum @rs; my @xs = map{ @$_ } @_; rdivide facproduct( @rs, @cs ), facproduct( $n, @xs ); } sub rproduct{ reduce{ rtimes $a, $b } [[],[]], @_ } sub facproduct{ rproduct map{ fac $_ } @_ } sub fac{ $_[0] < 2 ? [[],[]] : [[ 2 .. $_[0] ], []]; } sub toRatio{ [ bigProduct( $_[0][0] ), bigProduct( $_[0][1] ) ] } sub bigProduct{ reduce{ $a * $b } map{ @$_ } @_ } sub rdivide{ rtimes $_[0], [ $_[1][1], $_[1][0] ] } sub rtimes{ [ cancel merge($_[0][0], $_[1][0]), merge($_[0][1], $_[1][ +1] )] } sub merge; sub merge{ return $_[1] unless defined $_[0] and @{ $_[0] }; return $_[0] unless defined $_[1] and @{ $_[1] }; my( $x, @xs ) = @{ $_[0] }; my( $y, @ys ) = @{ $_[1] }; $x < $y ? [ $x, @{ merge( \@xs, [ $y, @ys ] ) } ] : [ $y, @{ merge( [ $x, @xs ], \@ys ) } ] } sub cancel; sub cancel{ return @_ unless @{ $_[0] } and @{ $_[1] }; my( $x, @xs ) = @{ $_[0] }; my( $y, @ys ) = @{ $_[1] }; return cancel \@xs, \@ys if $x == $y; return do{ my( $xs_, $ys_ ) = cancel \@xs, $_[1]; [$x, @{$xs_}], $ys_ }if $x < $y; return do{ my( $xs_,$ys_ ) = cancel $_[0], \@ys; $xs_, [$y, @{$ys_}] }; } package main; sub Scientific{ sprintf '%.17f', $_[0][0] / $_[0][1] } print Scientific FET::pCutoff [ 5, 0], [1, 4]; #print Scientific FET::pCutoff [ 989, 9400 ], [ 43300, 2400];

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.
  • Comment on Re^8: Algorithm for cancelling common factors between two lists of multiplicands (192.5 ms)
  • Select or Download Code