Python speed vs csharp

Siegfried Gonzi siegfried.gonzi at
Mon Aug 4 00:40:11 CEST 2003

Mike wrote:

> Ugh. Because I can't be trusted with a calculator, that's why. 1.5 billion
> should be 150 million. The elapsed times are correct: 210 seconds and 3500
> seconds. 

I cannot see your original message except on google:


I am sure you know what you are after, but  Python for numerical
computations is more or less crap. Not only has Python a crippled
syntax it is most of the time dog slow and unpredictable. Python is
more or less a fraud. But I must admit it is sometimes immensly useful
due to its big pool of add-on libraries. But I would rather like to
see that all the manpower gets concentrated in languages which
deserve to be called a programming language (for example Scheme,
CommonLisp, Ada, C++,..).

I am not sure what exactly are your simulation problems and
requirements but the Bigloo (Scheme) compiler is very robust and I use
it every day for numerical computations. I once used Python but
quickly stopped using it and I have never looked back.

For example Bigloo lets you specify types. It is easy to specify types
in Bigloo and the code is transportable even, provided you write a
small macro where the compiler decides wheter it is Bigloo or
not. Normally, giving types in Bigloo puts you in the range of "only 2
times slower than C"; also for heavy numerical computations. The
following small Scheme program sums up your error function 10^6
times. Also included a C program. The timings on a simple Celeron
laptop are as follows:

g++ -O3 erf.c : 0.5 seconds
bigloo -Obench erf.scm: 1.1 seconds

The Bigloo code could be a bit improved because I do not know wheter
using (expn x 2.0) introduces a bottleneck in Bigloo. If there are only 
point calculations  involved Bigloo is as fast as C (see
comp.lang.scheme and my code for the Coyote Gulch benchmark; see also
slashdot). If you have big arrays Bigloo's gap to C is shifted towards
a factor of 2 or in hard cases 3. This is still fairly good.

Not to cheerlead Bigloo or Scheme here. If you are pragmatic stay with
Python it has really tons of add-on libraries and huge community.

S. Gonzi
;;;;;;;;;;;;;;;;; BIGLOO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module erfc2
    (option (set! *genericity* #f)))

(define (erfc x )
    (let* ((x (exact->inexact x))
	  (p 0.3275911)
	  (a1 0.254829592)
	  (a2 -0.284496736)
	  (a3 1.421413741)
	  (a4 -1.453152027)
	  (a5 1.061405429)
	  (t  (/fl 1.0
		   (+fl 1.0
		      (*fl p
	  (erfcx  (*fl
		   (*fl t
		      (+fl a1
			 (*fl t (+fl a2
				 (*fl t
				    (+fl a3
				       (*fl t
					  (+fl a4
					     (*fl t a5)))))))))
		   (exp (- (expt x 2.0))))))

(define  (do-it)
    (let ((erg 0.0))
       (do ((i 0 (+fx i 1)))
	  ((=fx i 1000000))
	  (set! erg (+fl erg (erfc 0.456))))

(print (do-it))
;;; C ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include <stdio.h>
#include <math.h>

double erfc( double x )
    double p, a1, a2, a3, a4, a5;
    double t, erfcx;

    p  =  0.3275911;
    a1 =  0.254829592;
    a2 = -0.284496736;
    a3 =  1.421413741;
    a4 = -1.453152027;
    a5 =  1.061405429;

    t = 1.0 / (1.0 + p*x);
    erfcx = ( (a1 + (a2 + (a3 +
			  (a4 + a5*t)*t)*t)*t)*t ) * exp(-pow(x,2.0));

    return erfcx;

int main()
    double erg=0.0;
    int i;

    for(i=0; i<1000000; i++)
       erg += erfc(0.456);

    return 1;

More information about the Python-list mailing list