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

Re^3: Operations with Extremely Large Numbers (BigApprox)

by davido (Cardinal)
on Nov 09, 2011 at 19:25 UTC ( [id://937193]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Operations with Extremely Large Numbers (BigApprox)
in thread Operations with Extremely Large Numbers

Yes.

use Math::BigApprox qw/Fact/; use Benchmark qw/cmpthese/; use Memoize; memoize( 'Fact', INSTALL => 'fastFact' ); our $test = 20000; print "$test!\t=>\t", Fact($test), "\n"; cmpthese ( -5, { Fact => sub{ my $a = Fact($test) }, fastFact => sub{ my $a = fastFact($test) }, } );

Results...

20000! => 1.819206e+77337 Rate Fact fastFact Fact 7.89/s -- -100% fastFact 297159/s 3766395% --

You almost need Math::BigApprox to deal with the percent improvement when memoized. A factorial is one of those "pure functions" where memoization pays off if you intend to use the same params more than once.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-20 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found