performance problem in python 2.2

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Fri Jul 26 18:36:24 EDT 2002


Fernando Perez <fperez528 at yahoo.com> writes:
> #!/usr/bin/perl
> 
> $p = 2**64;
> $c = $ARGV[0];
> 
> $n = 1;
> foreach $i (1..$c) {
>         $n = ($n * ($p-$i)) / $p
> }
> print 1-$n, "\n";
> 
> But again, there's NO WAY you are going to write perl code to do this at C 
> speed (short of a trick like inlining C in perl).

Does

  $c = 0.0 + $ARGV[0];

making $c a float instead of a string help?


Also:

 $p = 2**64;
 $c = $ARGV[0];
 $n = exp(-($c*$c) / (2*$p));
 print 1-$n, "\n";

should give a pretty close approximation faster than any of the
iterative methods listed.



More information about the Python-list mailing list