in reply to Equation - code review

The simplest way to speed this up would be to abandon the use of Math::BigInt. You use this in two places to raise numbers to a power:

$power=Math::BigInt->new($initial_index); $distance_index=($power->bpow($distance))->numify();

but you then immediately retrieve the result as a standard scalar without performing any check for overflow.

Assuming the value '4' is typical scale of magnitude for $distance aka $self{ distance } aka $distance_index,

then using Perl's built-in exponentiation operator,  $power ** $distance

cmpthese -3, { BigInt => q[ my $x = Math::BigInt->new( $_ )->bpow( 4 )->numify() for 2 .. +1e6; ], standard => q[ my $x = $_ ** 4 for 2 .. 1e6; ], };; (warning: too few iterations for a reliable count) s/iter BigInt standard BigInt 172 -- -100% standard 0.625 27472% --

will be at least 250 times quicker.

For sequences of length 10,000, standard math will safely accomodate distances of up to 78.

If you really need to use Math::BigInt for this calculation, then you should be doing the 1 / $distance_index part of your calculation before you numify the result of the previous step.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Equation - code review
by kulls (Hermit) on Aug 16, 2006 at 13:04 UTC
    Hi,
    Thanks for your reply.
    literally i don't know how to handle the built-ins exponent function.I can able to find only  Math::BigInt package. However I can't able to overright the object , instead of creating new objects every time in that module.This makes me very bad situation if my length of string id very high.
    . I have tested my code with in-built function and it is working fine.
    -kulls