[libmath-prime-util-perl] 50/72: Redo benchmark section

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:49:40 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.32
in repository libmath-prime-util-perl.

commit a2ab7c93395c5b87ce007e2c85d22ca9153ee748
Author: Dana Jacobsen <dana at acm.org>
Date:   Thu Sep 26 00:07:20 2013 -0700

    Redo benchmark section
---
 xt/small-is-next-prev.pl | 110 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 82 insertions(+), 28 deletions(-)

diff --git a/xt/small-is-next-prev.pl b/xt/small-is-next-prev.pl
old mode 100755
new mode 100644
index eaf7d28..3e496c8
--- a/xt/small-is-next-prev.pl
+++ b/xt/small-is-next-prev.pl
@@ -5,13 +5,13 @@ use Math::Prime::Util qw/:all/;
 use Time::HiRes qw(gettimeofday tv_interval);
 $| = 1;  # fast pipes
 
-my $mpu_limit = shift || 50_000_000;
+my $nprimes = shift || 50_000_000;
 
 # 1. forprimes does a segmented sieve and calls us for each prime.  This is
 #    independent of is_prime and the main sieve.  So for each entry let's
 #    compare next_prime and prev_prime.
 {
-  print "Using MPU forprimes to $mpu_limit\n";
+  print "Using MPU forprimes to $nprimes\n";
   my $start_time = [gettimeofday];
   my $nextprint = 5000000;
   my $n = 0;
@@ -20,10 +20,10 @@ my $mpu_limit = shift || 50_000_000;
     die "prev $n" unless prev_prime($_) == $n;
     $n = $_;
     if ($n > $nextprint) { print "$n..";  $nextprint += 5000000; }
-  } $mpu_limit;
+  } $nprimes;
   my $seconds = tv_interval($start_time);
-  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($mpu_limit));
-  printf "Success using forprimes to $mpu_limit.  %6.2f uSec/call\n", $micro_per_call;
+  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($nprimes));
+  printf "Success using forprimes to $nprimes.  %6.2f uSec/call\n", $micro_per_call;
 }
 
 print "\n";
@@ -32,9 +32,9 @@ print "\n";
 #    prev_prime and next_prime functions really fast since they just look in
 #    the cached sieve.
 {
-  print "Using MPU forprimes to $mpu_limit with prime_precalc\n";
+  print "Using MPU forprimes to $nprimes with prime_precalc\n";
   my $start_time = [gettimeofday];
-  prime_precalc($mpu_limit);
+  prime_precalc($nprimes);
   my $nextprint = 5000000;
   my $n = 0;
   forprimes {
@@ -42,31 +42,44 @@ print "\n";
     die "prev $n" unless prev_prime($_) == $n;
     $n = $_;
     if ($n > $nextprint) { print "$n..";  $nextprint += 5000000; }
-  } $mpu_limit;
+  } $nprimes;
   my $seconds = tv_interval($start_time);
-  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($mpu_limit));
-  printf "Success using forprimes/precalc to $mpu_limit.  %6.2f uSec/call\n", $micro_per_call;
+  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($nprimes));
+  printf "Success using forprimes/precalc to $nprimes.  %6.2f uSec/call\n", $micro_per_call;
 }
 
 print "\n\n";
 
 # Now do some more comparative timing.
-my @pr = @{primes(10_000_000)};
+my @pr = @{primes($nprimes)};
 my $numpr = scalar @pr;
 prime_memfree();
 
 {
+  print "MPU             forprimes...";
+  my $start_time = [gettimeofday];
+  my $i = 0;
+  forprimes {
+    die "next $_ not ", $pr[$i-1] unless $pr[$i++] == $_;
+  } $nprimes;
+  my $seconds = tv_interval($start_time);
+  my $micro_per_call = ($seconds * 1000000) / (1*prime_count($nprimes));
+  printf "%8.2f uSec/call\n", $micro_per_call;
+  prime_memfree();
+}
+{
   print "MPU             prev/next...";
   my $start_time = [gettimeofday];
   my $n = 0;
   foreach my $p (@pr) {
     my $next = next_prime($n);
+    my $prev = prev_prime($p);
     die "MPU next($n) is not $p\n" unless $next == $p;
-    die "MPU prev($p) is not $n\n" unless $n == prev_prime($p);
+    die "MPU prev($p) is not $n\n" unless $prev == $n;
     $n = $next;
   }
   my $seconds = tv_interval($start_time);
-  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($numpr));
+  my $micro_per_call = ($seconds * 1000000) / (2*$numpr);
   printf "%8.2f uSec/call\n", $micro_per_call;
 }
 {
@@ -76,52 +89,93 @@ prime_memfree();
   my $n = 0;
   foreach my $p (@pr) {
     my $next = next_prime($n);
+    my $prev = prev_prime($p);
     die "MPU next($n) is not $p\n" unless $next == $p;
-    die "MPU prev($p) is not $n\n" unless $n == prev_prime($p);
+    die "MPU prev($p) is not $n\n" unless $prev == $n;
     $n = $next;
   }
   my $seconds = tv_interval($start_time);
-  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($numpr));
+  my $micro_per_call = ($seconds * 1000000) / (2*$numpr);
   printf "%8.2f uSec/call\n", $micro_per_call;
   prime_memfree();
 }
 
-# 3. Now Math::Pari.
+# Math::Prime::FastSieve
+if (eval { require Math::Prime::FastSieve; Math::Prime::FastSieve->import(); Inline->init(); 1; }) {
+  print "Math::Prime::FastSieve......";
+  my $start_time = [gettimeofday];
+  my $sieve = Math::Prime::FastSieve::Sieve->new( $pr[-1]+1000 );
+  my $n = 0;
+  foreach my $p (@pr) {
+    my $next = $sieve->nearest_ge($n+1);
+    my $prev = $sieve->nearest_le($p-1);
+    die "MPFS next($n) is not $p\n" unless $next == $p;
+    die "MPFS prev($p) is not $n\n" unless $prev == $n;
+    $n = $next;
+  }
+  my $seconds = tv_interval($start_time);
+  my $micro_per_call = ($seconds * 1000000) / (2*$numpr);
+  printf "%8.2f uSec/call\n", $micro_per_call;
+} else {
+  print "Math::Prime::FastSieve not installed.  Skipping\n";
+}
+
+# Math::Pari.
 if (eval { require Math::Pari; 1; }) {
   print "Math::Pari      prec/next...";
+  my @pari_pr = grep { $_ < 5_000_000 } @pr;
+  my $pari_numpr = scalar @pari_pr;
   my $start_time = [gettimeofday];
   my $n = 0;
-  foreach my $p (@pr) {
+  foreach my $p (@pari_pr) {
     my $next = Math::Pari::nextprime($n+1);
-    die "MPU next($n) is not $p\n" unless $next == $p;
-    die "MPU prev($p) is not $n\n" unless $n == Math::Pari::precprime($p-1);
+    my $prev = Math::Pari::precprime($p-1);
+    die "Pari next($n) is not $p\n" unless $next == $p;
+    die "Pari prec($p) is not $n\n" unless $prev == $n;
     $n = $next;
   }
   my $seconds = tv_interval($start_time);
-  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($numpr));
+  my $micro_per_call = ($seconds * 1000000) / (2*$pari_numpr);
   printf "%8.2f uSec/call\n", $micro_per_call;
 } else {
   print "Math::Pari not installed.  Skipping\n";
 }
 
-# 4. Math::Primality
+# Math::NumSeq::Primes
+if (eval { require Math::NumSeq::Primes; 1; }) {
+  print "Math::NumSeq::Primes next...";
+  my $start_time = [gettimeofday];
+  my $seq = Math::NumSeq::Primes->new();
+  my $n = 0;
+  foreach my $p (@pr) {
+    my $next = ($seq->next)[1];
+    die "MNP next($n) is not $p\n" unless $next == $p;
+    $n = $next;
+  }
+  my $seconds = tv_interval($start_time);
+  my $micro_per_call = ($seconds * 1000000) / (1*$numpr);
+  printf "%8.2f uSec/call\n", $micro_per_call;
+} else {
+  print "Math::NumSeq::Primes not installed.  Skipping\n";
+}
+
+# Math::Primality
 if (eval { require Math::Primality; 1; }) {
   print "Math::Primality prev/next...";
-  my @mppr = @pr[0..50000];
-  my $nummppr = scalar @mppr;
+  my @mp_pr = grep { $_ < 100_000 } @pr;
+  my $mp_numpr = scalar @mp_pr;
   my $start_time = [gettimeofday];
   my $n = 0;
-  foreach my $p (@mppr) {
+  foreach my $p (@mp_pr) {
     my $next = Math::Primality::next_prime($n);
     my $prev = ($p == 2) ? 0 : Math::Primality::prev_prime($p);
-    die "MPU next($n) is not $p\n" unless $next == $p;
-    die "MPU prev($p) is not $n\n" unless $n == $prev;
+    die "MP next($n) is not $p\n" unless $next == $p;
+    die "MP prev($p) is not $n\n" unless $prev == $n;
     $n = $next;
   }
   my $seconds = tv_interval($start_time);
-  my $micro_per_call = ($seconds * 1000000) / (2*prime_count($nummppr));
+  my $micro_per_call = ($seconds * 1000000) / (2*$mp_numpr);
   printf "%8.2f uSec/call\n", $micro_per_call;
 } else {
   print "Math::Primality not installed.  Skipping\n";
 }
-

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list