performance problem in python 2.2

Jeff Davis jdavis at empires.org
Fri Jul 26 16:25:26 EDT 2002


I wrote a small python program to help me solve a math problem. When I 
tried to run it with large values, it ate all my RAM and I eventually had 
to kill it. I tried writing the same thing in C and it used almost no RAM 
(and had an upper limit) and finished much faster. 

Then I was talking to someone who suggested that I try perl. I have the 
exact same algorithm in perl, and it doesn't eat my RAM, and executes much 
more quickly (same order of magnitude as the c program). It seems almost 
as if there's a memory leak in one of python's simple math operations, 
because it is so much worse than the other ones I tried. 

I have listed the two programs below. Does someone think I found a 
bug/memory leak? Does someone know about something else that might be 
going on?

Thanks,
        Jeff

===================python========================
#!/usr/bin/python2.2

import sys

n = 1.0
p = 2L**64
c = long(sys.argv[1],10)

for i in range(1,c):
    n = (n * (p-i)) / p
print 1-n
====================perl==========================
#!/usr/bin/perl

$p = 2**64;
$c = $ARGV[0];

$n = 1;
foreach $i (1..$c) {
        $n = ($n * ($p-$i)) / $p
}
print 1-$n, "\n";



More information about the Python-list mailing list