in reply to Help w/ Code Optimization
The idea behind, is that '**' is MUCH slower than '*', so I exchange 2 '**' for one '**' and 2 '*'sub Root2 { my $num = shift; my $root = shift; my $iterations = shift || 10; if ( $root == 0 ) { return 1 } if ( $num < 0 ) { return undef } my $current = Math::BigFloat->new(); my $guess = Math::BigFloat->new( $num / $root ); my $t=Math::BigFloat->new($guess**($root-1)); 1st version : WRONG ! sh +ould be in the loop. for ( 1 .. $iterations ) { $current = $guess - ( $guess * $t - $num ) / ( $root * $t ); if ( $guess eq $current ) { last } $t=Math::BigFloat->new($current**($root-1)); $guess = $current; } return $current; }
timethese(10,{ 'Root'=> sub {Root(1000,60)}, 'Root2'=> sub {Root2(1000,60)}});
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Help w/ Code Optimization
by sifukurt (Hermit) on Dec 26, 2001 at 22:58 UTC |