performance problem in python 2.2

Jeff Davis jdavis at empires.org
Fri Jul 26 18:05:39 EDT 2002


Thanks very much for yor reply, now the python one works much better. 
However, there is still quite a discrepency between python and perl. Of 
course C is fastest. I listed the times of execution below. I feel much 
better about python now.

Thanks again,
        Jeff

=============c====================
jdavis at jeff:~/code/c$ time ./crunch
0.000271013814962212042265765621351647496339865028858184814453125000000000000

real    0m32.915s
user    0m32.310s
sys     0m0.030s
=============perl==================
jdavis at jeff:~$ time code/pl/crunch.pl 100000000
0.000271014079116894

real    2m12.835s
user    2m8.400s
sys     0m0.110s
=============python=================
jdavis at jeff:~$ time code/py/crunch.py 100000000
0.000271014073697

real    13m19.242s
user    12m53.200s
sys     0m2.420s


Paul Rubin wrote:

> Jeff Davis <jdavis at empires.org> writes:
>> c = long(sys.argv[1],10)
>> 
>> for i in range(1,c):
>>     n = (n * (p-i)) / p
>> print 1-n
> 
> The problem is range(1,c) builds up a c-element list in memory.  Use
> xrange instead, which makes an object holding only the current
> iteration value.
> 
> For extra credit, solve the problem without a computer.  E.g., suppose
> you want to know the sum 1+2+3+...+999+1000.  You can add it up with a
> program or you can do it without a computer with a little cleverness.
> 
> Solving your birthday paradox problem needs a little bit more math
> than 1+2+3+...+1000, and the answer you get will be a close
> approximation rather than exact, but with similar cleverness you
> should be able to get a fairly simple formula.




More information about the Python-list mailing list