Python speed vs csharp
Michele Simionato
mis6 at pitt.edu
Mon Aug 4 15:06:39 EDT 2003
Alex Martelli <aleax at aleax.it> wrote in message news:<0MoXa.21930$cl3.850221 at news2.tin.it>...
> import math
>
> def erfc(x):
> exp = math.exp
>
> 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(-x*x)
> return erfcx
>
> def main():
> erg = 0.0
>
> for i in xrange(1000000):
> erg += erfc(0.456)
>
> print "%f" % erg
>
> if __name__ == '__main__':
> main()
>
Just for fun, I tested your scripts on my machine and actually my
results also substain the (heretic?) argument that Python (with psyco)
is *faster* than C for numeric computations:
Time Relative to (optimized) C
python+psyco 0.41s 0.59
c (-O option) 0.69s 1
c (normal) 0.80s 1.15
pure python 17.8s 26
This means that (in this computation) normal Python is 26 times
slower than optimized C; however Psyco gives a 43x speed-up and it
ends up to be 70% faster!
It took my breath away.
Kudos to Armin Rigo and Psyco!
Michele
P.S. Red Hat Linux 7.2, 500 GHz PIII, time measured with 'time',
Python 2.3 and Psyco 1.0, Alex Martelli scripts (with pow(x,2)-> x*x).
More information about the Python-list
mailing list