Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

<strike>6 times faster?</strike> (Re: Help w/ Code Optimization)

by arhuman (Vicar)
on Dec 26, 2001 at 22:27 UTC ( #134454=note: print w/ replies, xml ) Need Help??


in reply to Help w/ Code Optimization

You might also try this :

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; }
The idea behind, is that '**' is MUCH slower than '*', so I exchange 2 '**' for one '**' and 2 '*'
It seems indeed to speed things alot :
(I'm sure your numerous tests will make it sure ;-)
timethese(10,{ 'Root'=> sub {Root(1000,60)}, 'Root2'=> sub {Root2(1000,60)}});

gives as result :

  Benchmark: timing 10 iterations of Root, Root2...
    Root: 104 wallclock secs (104.38 usr + 0.01 sys = 104.39 CPU) @ 0.10/s (n=10)
    Root2: 15 wallclock secs (14.75 usr + 0.00 sys = 14.75 CPU) @ 0.68/s (n=10)
    Root2: 97 wallclock secs (92.67 usr + 0.16 sys = 92.83 CPU) @ 0.11/s (n=10)

UPDATE :
Corrected my code! I misplaced the $t calculation outside the loop,
which is wrong beccause $guess change inside the loop.
As you can see it's no more so fast...

"Only Bad Coders Code Badly In Perl" (OBC2BIP)


Comment on <strike>6 times faster?</strike> (Re: Help w/ Code Optimization)
Select or Download Code
Re: Re: Help w/ Code Optimization
by sifukurt (Hermit) on Dec 26, 2001 at 22:58 UTC
    When I try to run your code, I get an incorrect answer for the root. I think the problem is in the exponent. The exponents are $root and ($root - 1), respectively.

    When I run this code:
    timethese( 100, { 'Root' => '$x = Root( 100, 3 )', 'Root2' => '$y = Root2( 100, 3 )', }); print <<END; Root: $x ----- Root2: $y END
    I get this for output:
    Benchmark: timing 100 iterations of Root, Root2... Root: 29 wallclock secs (28.39 usr + 0.00 sys = 28.39 CPU) @ 3 +.52/s (n=1 00) Root2: 20 wallclock secs (20.03 usr + 0.00 sys = 20.03 CPU) @ 4 +.99/s (n=1 00) Root: 4.6415888336127788924100763542328778501807372125002714 ----- Root2: 0.6664902595019951166742874561807256134735

    ___________________
    Kurt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2015-07-05 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls