# Python is darn fast (was: How fast is Python)

Michele Simionato mis6 at pitt.edu
Sat Aug 23 17:37:44 CEST 2003

```I posted this few weeks ago (remember the C Sharp thread?) but it went
unnoticed on the large mass of posts, so let me retry. Here I get Python+
Psyco twice as fast as optimized C, so I would like to now if something
is wrong on my old laptop and if anybody can reproduce my results.
Here are I my numbers for calling the error function a million times
(Python 2.3, Psyco 1.0, Red Hat Linux 7.3, Pentium II 366 MHz):

\$ time p23 erf.py
real    0m0.614s
user    0m0.551s
sys     0m0.029s

This is twice as fast as optimized C:

\$ gcc erf.c -lm -o3
\$ time ./a.out
real    0m1.125s
user    0m1.086s
sys     0m0.006s

Here is the situation for pure Python

\$time p23 erf.jy
real    0m25.761s
user    0m25.012s
sys     0m0.049s

and, just for fun, here is Jython performance:

\$ time jython erf.jy
real    0m42.979s
user    0m41.430s
sys     0m0.361s

The source code follows (copied from Alex Martelli's post):

----------------------------------------------------------------------

\$ cat erf.py
import math
import psyco
psyco.full()

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)

if __name__ == '__main__':
main()

--------------------------------------------------------------------------

# python/jython version = same without "import psyco; psyco.full()"

--------------------------------------------------------------------------

\$cat erf.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(-x*x);

return erfcx;
}

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

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

return 0;
}

Michele Simionato, Ph. D.
MicheleSimionato at libero.it
http://www.phyast.pitt.edu/~micheles
--- Currently looking for a job ---

```