performance problem in python 2.2

Tim Peters tim.one at comcast.net
Fri Jul 26 16:55:41 EDT 2002


[Jeff Davis]
> I wrote a small python program to help me solve a math problem. When I
> tried to run it with large values,

What does "large values" mean?  Please be specific.

> 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.

When you wrote it in C, how did you declare p?  In your Perl code $p is a
double-precision float, and in your Python code p is an unbounded integer (a
data type Perl doesn't have natively).  It's impossible to know what you
intended (although easy to guess <wink>).

> 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?

I don't, no.

> Does someone know about something else that might be going on?

You do, yes -- but you haven't told us what yet <wink>.

> ...
> ===================python========================
> #!/usr/bin/python2.2
>
> import sys
>
> n = 1.0
> p = 2L**64

If you intend this to be a float, say

  p = 2.0**64

instead.

> c = long(sys.argv[1],10)
>
> for i in range(1,c):

This may or may not be troublesome, depending on what you meant by "large
values".  You could try using xrange instead.  Python code also runs
significantly faster if loops are in functions (local variable access is
significantly quicker than global variable access in Python).

>     n = (n * (p-i)) / p
> print 1-n
> ====================perl==========================
> #!/usr/bin/perl
>
> $p = 2**64;

Here $p is a float, although it "looks like" an integer.  You should be
aware that, because it's a float, your later $p-$i computes exactly $p until
$i gets large enough to affect $p's least-significant bit.

> $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