performance problem in python 2.2
Jeff Davis
jdavis at empires.org
Fri Jul 26 20:14:36 EDT 2002
Paul Rubin wrote:
> 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.
Very nice! unfortunately, when I started to solve the problem I didn't
understand the relationship to e with my algorithm. Upon further
mathematical analysis (after seeing a little pattern develop), I realized
the relationship. Of course, that still left me with my python questions
:)
I learned to never underestimate the usefulness of mathematics in
programming. Lucky for me, I had a teacher to ask and he gave me a lot of
good info as well. After giving me some advice (including CS related tips
that mostly boiled down to "Use assembly, or maybe C if you can about
getting an answer"), he pointed me to Sterling's Theorem, which seems
similar to what you did.
Thanks!
Regards,
Jeff
More information about the Python-list
mailing list