Permalink
Browse files

Add benchmarks comparing each Math::Primality function to Math::Prime…

…::Util
  • Loading branch information...
1 parent d8ad6cd commit 148ecbca55c00ec9a4ccbd3292d7035abc188b29 @danaj danaj committed with Feb 4, 2013
Showing with 134 additions and 0 deletions.
  1. +25 −0 bench/bench-mp-isprime.pl
  2. +29 −0 bench/bench-mp-lucas.pl
  3. +29 −0 bench/bench-mp-nextprime.pl
  4. +22 −0 bench/bench-mp-primecount.pl
  5. +29 −0 bench/bench-mp-psrp.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use lib 'lib';
+use Math::Prime::Util;
+use Math::Prime::Util::GMP;
+use Math::Primality;
+use Benchmark qw/:all/;
+my $count = shift || -2;
+srand(29); # So we have repeatable results
+
+test_at_digits($_, 1000) for (5, 15, 25, 50, 200);
+
+sub test_at_digits {
+ my($digits, $numbers) = @_;
+ die "Digits must be > 0" unless $digits > 0;
+
+ my @nums = map { Math::Prime::Util::random_ndigit_prime($digits)+2 } 1 .. $numbers;
+ print "is_prime for $numbers random $digits-digit numbers\n";
+
+ cmpthese($count,{
+ 'MP' => sub { Math::Primality::is_prime($_) for @nums; },
+ 'MPU' => sub { Math::Prime::Util::is_prob_prime($_) for @nums; },
+ });
+}
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use lib 'lib';
+use Math::Prime::Util;
+use Math::Prime::Util::GMP;
+use Math::Primality;
+use Benchmark qw/:all/;
+my $count = shift || -2;
+srand(29); # So we have repeatable results
+
+test_at_digits($_, 1000) for (5, 15, 25, 50, 200);
+
+sub test_at_digits {
+ my($digits, $numbers) = @_;
+ die "Digits must be > 0" unless $digits > 0;
+
+ # We get a mix of primes and non-primes.
+ my @nums = map { Math::Prime::Util::random_ndigit_prime($digits)+2 } 1 .. $numbers;
+ print "is_strong_lucas_pseudoprime for $numbers random $digits-digit numbers\n";
+ print "BigInt backend: ", $nums[0]->config()->{lib}, " v", $nums[0]->config()->{lib_version}, "\n" if ref($nums[0]) eq 'Math::BigInt';
+
+ cmpthese($count,{
+ 'MP' =>sub {Math::Primality::is_strong_lucas_pseudoprime($_) for @nums;},
+ 'MPU' =>sub {Math::Prime::Util::is_strong_lucas_pseudoprime($_) for @nums;},
+ 'MPU PP' =>sub {Math::Prime::Util::PP::is_strong_lucas_pseudoprime($_) for @nums;},
+ 'MPU GMP' =>sub {Math::Prime::Util::GMP::is_strong_lucas_pseudoprime($_) for @nums;},
+ });
+}
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use lib 'lib';
+use Math::Prime::Util;
+use Math::Prime::Util::GMP;
+use Math::Primality;
+use Benchmark qw/:all/;
+my $count = shift || -2;
+srand(29); # So we have repeatable results
+
+test_at_digits($_, 1000) for (5, 15, 25, 50, 200);
+
+sub test_at_digits {
+ my($digits, $numbers) = @_;
+ die "Digits must be > 0" unless $digits > 0;
+
+ my $start = Math::Prime::Util::random_ndigit_prime($digits) - 3;
+ my $end = $start;
+ $end = Math::Prime::Util::GMP::next_prime($end) for 1 .. $numbers;
+
+ print "next_prime x $numbers starting at $start\n";
+
+ cmpthese($count,{
+ 'MP' => sub { my $n = $start; $n = Math::Primality::next_prime($n) for 1..$numbers; die "MP ended with $n instead of $end" unless $n == $end; },
+ 'MPU' => sub { my $n = $start; $n = Math::Prime::Util::next_prime($n) for 1..$numbers; die "MPU ended with $n instead of $end" unless $n == $end; },
+ 'MPU GMP' => sub { my $n = $start; $n = Math::Prime::Util::GMP::next_prime($n) for 1..$numbers; die "MPU GMP ended with $n instead of $end" unless $n == $end; },
+ });
+}
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use lib 'lib';
+use Math::Prime::Util;
+use Math::Prime::Util::GMP;
+use Math::Primality;
+use Benchmark qw/:all/;
+my $count = shift || -2;
+
+#my($n, $exp) = (100000,9592);
+#my($n, $exp) = (1000000,78498);
+my($n, $exp) = (10000000,664579);
+cmpthese($count,{
+ 'MP' =>sub { die unless $exp == Math::Primality::prime_count($n); },
+ 'MPU default' =>sub { die unless $exp == Math::Prime::Util::prime_count($n); },
+ 'MPU XS Sieve' =>sub { die unless $exp == Math::Prime::Util::_XS_prime_count($n); },
+ 'MPU XS Lehmer'=>sub { die unless $exp == Math::Prime::Util::_XS_lehmer_pi($n); },
+ 'MPU PP Sieve' =>sub { die unless $exp == Math::Prime::Util::PP::_sieve_prime_count($n); },
+ 'MPU PP Lehmer'=>sub { die unless $exp == Math::Prime::Util::PP::_lehmer_pi($n); },
+ 'MPU GMP Trial'=>sub { die unless $exp == Math::Prime::Util::GMP::prime_count(2,$n); },
+});
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use lib 'lib';
+use Math::Prime::Util;
+use Math::Prime::Util::GMP;
+use Math::Primality;
+use Benchmark qw/:all/;
+my $count = shift || -2;
+srand(29); # So we have repeatable results
+
+test_at_digits($_, 1000) for (5, 15, 25, 50, 200);
+
+sub test_at_digits {
+ my($digits, $numbers) = @_;
+ die "Digits must be > 0" unless $digits > 0;
+
+ # We get a mix of primes and non-primes.
+ my @nums = map { Math::Prime::Util::random_ndigit_prime($digits)+2 } 1 .. $numbers;
+ print "is_strong_pseudoprime for $numbers random $digits-digit numbers\n";
+ print "BigInt backend: ", $nums[0]->config()->{lib}, " v", $nums[0]->config()->{lib_version}, "\n" if ref($nums[0]) eq 'Math::BigInt';
+
+ cmpthese($count,{
+ 'MP' =>sub {Math::Primality::is_strong_pseudoprime($_,3) for @nums;},
+ 'MPU' =>sub {Math::Prime::Util::is_strong_pseudoprime($_,3) for @nums;},
+ 'MPU PP' =>sub {Math::Prime::Util::PP::miller_rabin($_,3) for @nums;},
+ 'MPU GMP' =>sub {Math::Prime::Util::GMP::is_strong_pseudoprime($_,3) for @nums;},
+ });
+}

0 comments on commit 148ecbc

Please sign in to comment.