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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Equation - code review
by kulls (Hermit) on Aug 16, 2006 at 13:04 UTC |