in reply to bignum rocks 2^43_112_609-1

Unfortunally, bignum is also kind of slow. Take the following program:
use 5.010; use bignum; use warnings; use strict; my $x = shift || 100; my $y = 2**$x - 1; say $y;
calling it with 500000 as argument took 3m02s on my box (output redirected to /dev/null). The equivalent
echo '2^500000-1'|bc > /dev/null
only took 4 seconds.

So, in general, if I want arbitrary precision numbers, I use bc instead of bignum.

Replies are listed 'Best First'.
Re^2: bignum rocks 2^43_112_609-1
by salva (Canon) on Sep 29, 2008 at 15:09 UTC
    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!!!
      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.
Re^2: bignum rocks 2^43_112_609-1
by jettero (Monsignor) on Sep 29, 2008 at 15:03 UTC
    Wow, that's a huge difference. Is this a GMPlib problem or a bignum.pm problem? That's the question in my mind.

    -Paul