[libmath-prime-util-perl] 10/14: Allow turning on/off gnufactor or pari
Partha P. Mukherjee
ppm-guest at moszumanska.debian.org
Thu May 21 18:47:53 UTC 2015
This is an automated email from the git hooks/post-receive script.
ppm-guest pushed a commit to annotated tag v0.25
in repository libmath-prime-util-perl.
commit 5a80aebfb55ccb71bbe9d9678e7f3b99dbee928d
Author: Dana Jacobsen <dana at acm.org>
Date: Mon Mar 18 18:20:57 2013 -0700
Allow turning on/off gnufactor or pari
---
examples/test-factor-gnufactor.pl | 99 +++++++++++++++++++++++++++------------
1 file changed, 70 insertions(+), 29 deletions(-)
diff --git a/examples/test-factor-gnufactor.pl b/examples/test-factor-gnufactor.pl
index dc9c6af..8f0e9b9 100755
--- a/examples/test-factor-gnufactor.pl
+++ b/examples/test-factor-gnufactor.pl
@@ -14,6 +14,27 @@ $| = 1; # fast pipes
srand(87431);
my $num = 1000;
+# Note: If you have factor from coreutils 8.20 or later (e.g. you're running
+# Fedora), then GNU factor will be very fast and support at least 128-bit
+# inputs (~44 digits). Its growth is not great however, so 25+ digits starts
+# getting slow. The authors wrote on a forum that a future version will
+# include a TinyQS, which should make it really rock for medium-size inputs.
+#
+# On the other hand, if you have the older factor (e.g. you're running
+# Ubuntu) then GNU factor uses trial division so will be very painful for
+# large numbers. You'll probably want to turn it off here as it will be
+# many thousands of times slower than MPU and Pari.
+
+# A performance note: MPU and Pari get their results by calling a function.
+# GNU factor gets its result by multiple shells out to /usr/bin/factor with
+# the numbers as command line arguments. This adds a lot of overhead that
+# has nothing to do with their implementation. For comparison, try turning
+# on the MPU factor.pl script, and weep for Perl's startup cost.
+
+my $do_gnu = 1;
+my $do_pari = 1;
+my $use_mpu_factor_script = 0;
+
my $rgen = sub {
my $range = shift;
return 0 if $range <= 0;
@@ -46,47 +67,56 @@ foreach my $digits (5 .. $maxdigits) {
sub test_array {
my @narray = @_;
my($start, $mpusec, $gnusec, $parisec, $diff);
+ my(@mpuarray, @gnuarray, @pariarray);
print ".";
$start = [gettimeofday];
- my @mpuarray = mpu_factors(@narray);
+ @mpuarray = mpu_factors(@narray);
$mpusec = tv_interval($start);
- print ".";
- $start = [gettimeofday];
- my @gnuarray = gnu_factors(@narray);
- $gnusec = tv_interval($start);
+ if ($do_gnu) {
+ print ".";
+ $start = [gettimeofday];
+ @gnuarray = gnu_factors(@narray);
+ $gnusec = tv_interval($start);
+ }
- print ".";
- $start = [gettimeofday];
- my @pariarray = pari_factors(@narray);
- $parisec = tv_interval($start);
+ if ($do_pari) {
+ print ".";
+ $start = [gettimeofday];
+ @pariarray = pari_factors(@narray);
+ $parisec = tv_interval($start);
+ }
print ".";
die "MPU got ", scalar @mpuarray, " factors. GNU factor got ",
- scalar @gnuarray, "\n" unless $#mpuarray == $#gnuarray;
+ scalar @gnuarray, "\n" unless !$do_gnu || $#mpuarray == $#gnuarray;
die "MPU got ", scalar @mpuarray, " factors. Pari factor got ",
- scalar @pariarray, "\n" unless $#mpuarray == $#pariarray;
+ scalar @pariarray, "\n" unless !$do_pari || $#mpuarray == $#pariarray;
foreach my $n (@narray) {
my @mpu = @{shift @mpuarray};
- my @gnu = @{shift @gnuarray};
- my @pari = @{shift @pariarray};
die "mpu array is for the wrong n?" unless $n == shift @mpu;
- die "gnu array is for the wrong n?" unless $n == shift @gnu;
- die "pari array is for the wrong n?" unless $n == shift @pari;
- $diff = diff \@mpu, \@gnu, { STYLE => 'Table' };
- die "factor($n): MPU/GNU\n$diff\n" if length($diff) > 0;
- my $diff = diff \@mpu, \@pari, { STYLE => 'Table' };
- die "factor($n): MPU/Pari\n$diff\n" if length($diff) > 0;
+ if ($do_gnu) {
+ my @gnu = @{shift @gnuarray};
+ die "gnu array is for the wrong n?" unless $n == shift @gnu;
+ $diff = diff \@mpu, \@gnu, { STYLE => 'Table' };
+ die "factor($n): MPU/GNU\n$diff\n" if length($diff) > 0;
+ }
+ if ($do_pari) {
+ my @pari = @{shift @pariarray};
+ die "pari array is for the wrong n?" unless $n == shift @pari;
+ my $diff = diff \@mpu, \@pari, { STYLE => 'Table' };
+ die "factor($n): MPU/Pari\n$diff\n" if length($diff) > 0;
+ }
}
print ".";
# We should ignore the small digits, since we're comparing direct
# Perl functions with multiple command line invocations. It really
# doesn't make sense until we're over 1ms per number.
- printf "OK MPU:%7.2f ms GNU:%7.2f ms Pari:%7.2f ms\n",
- (($mpusec*1000) / scalar @narray),
- (($gnusec*1000) / scalar @narray),
- (($parisec*1000) / scalar @narray);
+ printf "OK MPU:%8.3f ms", (($mpusec*1000) / scalar @narray);
+ printf(" GNU:%8.3f ms", (($gnusec*1000) / scalar @narray)) if $do_gnu;
+ printf(" Pari:%8.3f ms", (($parisec*1000) / scalar @narray)) if $do_pari;
+ print "\n";
}
sub gendigits {
@@ -109,7 +139,22 @@ sub gendigits {
sub mpu_factors {
my @piarray;
- push @piarray, [$_, factor($_)] for @_;
+
+ if (!$use_mpu_factor_script) {
+ push @piarray, [$_, factor($_)] for @_;
+ } else {
+ my @ns = @_;
+ my $numpercommand = int( (4000-30)/(length($ns[-1])+1) );
+ while (@ns) {
+ my $cs = join(" ", '/usr/bin/factor', splice(@ns, 0, $numpercommand));
+ my $fout = qx{$cs};
+ my @flines = split(/\n/, $fout);
+ foreach my $fline (@flines) {
+ $fline =~ s/^(\d+): //;
+ push @piarray, [$1, split(/ /, $fline)];
+ }
+ }
+ }
@piarray;
}
@@ -119,11 +164,7 @@ sub gnu_factors {
my $numpercommand = int( (4000-30)/(length($ns[-1])+1) );
while (@ns) {
- my $cs = '/usr/bin/factor';
- foreach my $n (1 .. $numpercommand) {
- last unless @ns;
- $cs .= " " . shift @ns;
- }
+ my $cs = join(" ", '/usr/bin/factor', splice(@ns, 0, $numpercommand));
my $fout = qx{$cs};
my @flines = split(/\n/, $fout);
foreach my $fline (@flines) {
--
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