in reply to Re: bignum rocks 2^43_112_609-1
in thread bignum rocks 2^43_112_609-1

By default, bignum uses a backend module written in pure Perl to do the math, but you can instruct it to use a different implementation, for instance the fast GMP library (see this):

On my computer:

$ time echo '2^500000-1'|bc >/dev/null real 0m3.057s user 0m2.920s sys 0m0.010s $ time perl -e 'use bigint only => 'GMP'; my $x = 2 ** 500000 - 1; pri +nt $x' >/dev/null real 0m0.201s user 0m0.160s sys 0m0.010s
In that case, the perl version is ~15 times faster!!!

Replies are listed 'Best First'.
Re^3: bignum rocks 2^43_112_609-1
by jettero (Monsignor) on Sep 29, 2008 at 15:18 UTC
    See, I'm under the impression that bignum uses Math::BigInt, which uses Math::BigInt::GMP if available. Since I have Math::BigInt::GMP installed, I'd assume (by default) it'd use that. But otherwise, I see what you mean.

    It seems reasonable to me to have bigint require GMP. I wonder if there are cases where you'd really want to do bignum processing in pure Perl.

    UPDATE: this is false. bignum explicitly asks for ::Calc. Why would it do that?

    -Paul

      UPDATE: this is false. bignum explicitly asks for ::Calc. Why would it do that?
      Probably, just to avoid unexpected incompatibility issues between the backends that could result in hard to find bugs!
      It seems reasonable to me to have bigint require GMP

      Me, too !! But a part of the Math::BigInt charter is that the module should work with *just* perl installed - that no additional C library should be necessary. That immediately counts out GMP.

      For that matter, if you want to use GMP, there's no need to invoke bigint at all. You could just use Math::GMP (or, for access to the complete GMP functionality, <plug> Math::GMPf, Math::GMPq and Math::GMPz </plug>).

      I wonder if there are cases where you'd really want to do bignum processing in pure Perl

      For the occasional bignum operation, "pure perl" is not necssarily a high price to pay - maybe a few milliseconds more than the cost of loading GMP. (And maybe even a few milliseconds *less* ... faik :-)

      But, speaking for myself ... you're right ... *I* certainly don't want to do bignum processing in pure perl.

      Cheers,
      Rob
      The reason bignum most likely wants to be as pure perl as possible is because this means that anyone who can run perl can run bignum, regardless of their access to a compiler etc.

      The big advantage is that for instance a hosting provider that does not allow compiling of code on their machine (any smart one will not let you do that) will still let you install bignum, which might be slow but still is handy.
        Said smart hosting provider will almost certainly let you upload binaries though, so you could compile your modules (and most likely you're own perl as well) and upload that to your site dirs... If you couldn't do *that* it'd be time for a new one I imagine. Not that I've ever been in that kind of shared hosting. You can get virtual hosting for as little as $7/m these days...

        -Paul