File: small-is-next-prev.pl

package info (click to toggle)
libmath-prime-util-perl 0.73-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,796 kB
  • sloc: perl: 24,676; ansic: 11,471; makefile: 26; python: 24
file content (181 lines) | stat: -rwxr-xr-x 5,875 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env perl
use strict;
use warnings;
use Math::Prime::Util qw/:all/;
use Time::HiRes qw(gettimeofday tv_interval);
$| = 1;  # fast pipes

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 $nprimes\n";
  my $start_time = [gettimeofday];
  my $nextprint = 5000000;
  my $n = 0;
  forprimes {
    die "next $n not $_" unless next_prime($n) == $_;
    die "prev $n" unless $n == 0 || prev_prime($_) == $n;
    $n = $_;
    if ($n > $nextprint) { print "$n..";  $nextprint += 5000000; }
  } $nprimes;
  my $seconds = tv_interval($start_time);
  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";

# 2. Just like before, but now we'll call prime_precalc first.  This makes the
#    prev_prime and next_prime functions really fast since they just look in
#    the cached sieve.
{
  print "Using MPU forprimes to $nprimes with prime_precalc\n";
  my $start_time = [gettimeofday];
  prime_precalc($nprimes);
  my $nextprint = 5000000;
  my $n = 0;
  forprimes {
    die "next $n not $_" unless next_prime($n) == $_;
    die "prev $n" unless $n==0 || prev_prime($_) == $n;
    $n = $_;
    if ($n > $nextprint) { print "$n..";  $nextprint += 5000000; }
  } $nprimes;
  my $seconds = tv_interval($start_time);
  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($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==0 || $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;
}
{
  print "MPU precalc     prev/next...";
  my $start_time = [gettimeofday];
  prime_precalc($pr[-1]+1000);
  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==0 || $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;
  prime_memfree();
}

# 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 $n==0 || $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 (@pari_pr) {
    my $next = Math::Pari::nextprime($n+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 $n==0 || $prev == $n;
    $n = $next;
  }
  my $seconds = tv_interval($start_time);
  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";
}

# 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 @mp_pr = grep { $_ < 100_000 } @pr;
  my $mp_numpr = scalar @mp_pr;
  my $start_time = [gettimeofday];
  my $n = 0;
  foreach my $p (@mp_pr) {
    my $next = Math::Primality::next_prime($n);
    my $prev = ($p == 2) ? 0 : Math::Primality::prev_prime($p);
    die "MP next($n) is not $p\n" unless $next == $p;
    die "MP prev($p) is not $n\n" unless $n==0 || $prev == $n;
    $n = $next;
  }
  my $seconds = tv_interval($start_time);
  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";
}