in reply to Calling an overload::Method

I stumbled across it while trying to add some speedups to Math-GMP

Nice post ++, btw.
I've always assumed that if you want to get the maximum performance out of Math::GMP, then you'll avoid the overloading altogether and explicitly make the method calls.
I think this is generally true and the following script seems to support that view, though it only compares the 2 ways of performing a right shift.
use warnings; use Benchmark; use Math::GMP; no warnings 'once'; $str1 = '123456789' x 1000; $gmp1 = Math::GMP->new($str1); timethese(100000, { 'Math::GMP 1' => '$ret1 = $gmp1 >> 1000;', 'Math::GMP 2' => '$ret2 = $gmp1->div_2exp_gmp(1000)', }); print "gmp calculation ok\n" if $ret1 == $ret2; __END__ Outputs: Benchmark: timing 100000 iterations of Math::GMP 1, Math::GMP 2... Math::GMP 1: 0 wallclock secs ( 0.19 usr + 0.00 sys = 0.19 CPU) @ 5 +34759.36/s (n=100000) (warning: too few iterations for a reliable count) Math::GMP 2: 0 wallclock secs ( 0.12 usr + 0.00 sys = 0.12 CPU) @ 8 +00000.00/s (n=100000) (warning: too few iterations for a reliable count) gmp calculation ok
I might as well showcase my own Math::GMPz module while I'm at it, though I see it's main benefit as being that it enwraps much more of the gmp library than is covered by Math::GMP.
use warnings; use Benchmark; use Math::GMP; use Math::GMPz qw(:mpz); no warnings 'once'; $str1 = '123456789' x 1000; $gmp1 = Math::GMP->new ($str1); $gmpz1 = Math::GMPz->new($str1); $ret4 = Math::GMPz->new(); timethese(100000, { 'Math::GMP 1' => '$ret1 = $gmp1 >> 1000;', 'Math::GMP 2' => '$ret2 = $gmp1->div_2exp_gmp(1000);', 'Math::GMPz 1' => '$ret3 = $gmpz1 >> 1000;', 'Math::GMPz 2' => 'Rmpz_div_2exp($ret4, $gmpz1, 1000);', }); print "gmp calcualtion ok\n" if $ret1 == $ret2; print "gmpz calculation ok\n" if $ret3 == $ret4; print "both gmp and gmpz agree\n" if "$ret1" eq "$ret3"; __END__ Outputs: Benchmark: timing 100000 iterations of Math::GMP 1, Math::GMP 2, Math: +:GMPz 1, Math::GMPz 2... Math::GMP 1: 0 wallclock secs ( 0.19 usr + 0.00 sys = 0.19 CPU) @ 5 +34759.36/s (n=100000) (warning: too few iterations for a reliable count) Math::GMP 2: 0 wallclock secs ( 0.12 usr + 0.00 sys = 0.12 CPU) @ 8 +00000.00/s (n=100000) (warning: too few iterations for a reliable count) Math::GMPz 1: 0 wallclock secs ( 0.09 usr + 0.00 sys = 0.09 CPU) @ +1063829.79/s (n=100000) (warning: too few iterations for a reliable count) Math::GMPz 2: 0 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU) @ +3225806.45/s (n=100000) (warning: too few iterations for a reliable count) gmp calcualtion ok gmpz calculation ok both gmp and gmpz agree
Both Math::GMP and Math::GMPz were built against the very same gmp-6.2.1 library.

Cheers,
Rob