zentara has asked for the wisdom of the Perl Monks concerning the following question:
This is a script which divides a sequence of numbers by pi, using very high precision, provided by syphilis 's nice port of the Multiple Precision Float Library , which is at Math-MPFR-1.10.
I was making a comparison of speeds between c and Perl, and made 2 identical scripts, 1 c, and 1 Perl. The c script outputs as I expect, i.e 1 number per line. The Perl script outputs correct values, but the output comes in chunks of numbers, followed by chunks of newlines. If you look at the output loop, you can see that I print a newline after each number, but apperently Perl is saving them up and printing them all at once, instead of interleaving them into the output. This is totally bizarre behavior which I have never seen.
Can anyone explain why Perl isn't immediately putting the newline out, before continuing the loop?
#!/usr/bin/perl use warnings; use strict; use Math::MPFR qw(:mpfr); use FileHandle; $|++; use constant MY_PI => 3.1415926535897932384626433832795028841971693993 +751; Rmpfr_set_default_prec(256); print Rmpfr_get_default_prec(),"\n"; my $n = Rmpfr_init2(256); # numerator + my $d = Rmpfr_init2(256); # denominator + my $r = Rmpfr_init2(256); # result + my @barray; Rmpfr_set_d ($d, MY_PI , GMP_RNDD); + my $s3 = Rmpfr_get_str($d,10,0, GMP_RNDD); print "$s3\n"; for (my $i = 0; $i < 1000; $i++){ $barray[$i] = Rmpfr_init2( 256 ); #init array element Rmpfr_set_d ($n, $i , GMP_RNDD); + Rmpfr_div ($barray[$i], $n, $d, GMP_RNDD); } print "check mem, then hit a key\n"; <>; for (my $i = 0; $i < 1000; $i++){ Rmpfr_out_str($barray[$i],10,0,GMP_RNDD); syswrite STDOUT ,"\n"; # print "\n" dosn't work either STDOUT->flush(); } __END__
The bad output looks something like this: a bunch of numbers strung together a bunch of blank lines a bunch of numbers strung together a bunch of blank lines ....... ......
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: xs outputs faster than perl can keep up
by syphilis (Archbishop) on Feb 05, 2007 at 23:59 UTC | |
by zentara (Cardinal) on Feb 06, 2007 at 13:11 UTC | |
by syphilis (Archbishop) on Feb 06, 2007 at 13:59 UTC | |
Re: xs outputs faster than perl can keep up
by almut (Canon) on Feb 05, 2007 at 22:54 UTC | |
by syphilis (Archbishop) on Feb 07, 2007 at 05:44 UTC | |
Re: xs outputs faster than perl can keep up
by diotalevi (Canon) on Feb 05, 2007 at 22:41 UTC |