performance problem in python 2.2
Siegfried Gonzi
siegfried.gonzi at kfunigraz.ac.at
Sun Jul 28 05:09:30 EDT 2002
Jeff Davis wrote:
> 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.
If you need a good compiler but want not give up the convenience of a
dynamically typed language you should take a look at Bigloo. Bigloo is a
Scheme compiler which produces fast executables:
==
n = 1.0
p = 2.0**64
c = 1000000.0
for i in xrange(1,c+1):
n = (n * (p - 1)) / p
print (1 - n)
==
time python bench_py.py
real
0m17.214s
user 0m17.190s
sys 0m0.000s
==
(module bench)
(define (sum-silly1 c)
(let* ((n 1.0)
(p (expt 2.0 64.0)))
(do ((i 1 (+ i 1)))
((= i (+ i 1)))
(set! n (/ (* n (- p i))
p)))
(print (- 1 n))))
(define (sum-silly2 c)
(let* ((n 1.0)
(p (expt 2.0 64.0)))
(do ((i 1 (+fx i 1)))
((=fx i (+fx (flonum->fixnum c) 1)))
(set! n (/fl (*fl n (-fl p (fixnum->flonum i)))
p)))
(print (- 1 n))))
(sum-silly1 1000000.0)
;;(sum-silly2 1000000.0)
==
bigloo -Obench bench.scm
time ./a.out
real 0m0.720s
user 0m0.690s
sys 0m0.030s
or the second version with native Bigloo floating point operations:
bigloo -Obench bench.scm
time ./a.out
real 0m0.073s
user 0m0.060s
sys 0m0.010se
==
#include <stdlib.h>
#include <stdio.h>
int main()
{
long i,c;
double n = 1.0;
double p = 1.844674407370955161e+19;
c = 1000000.0
for (i=1;i<=c;++i)
{
n = (n * (p - i)) / p;
}
printf("%.16e\n",(1-n));
return 0;
}
==
gcc -O3 bench_c.c
time ./a.out
real 0m0.017s
user 0m0.010s
sys 0m0.000s
Bigloo is not only good at microbenchmarks it performs well on arrays
and lists too.
I am about to release the DISLIN binding to Bigloo in a few weeks.
S. Gonzi
More information about the Python-list
mailing list