Python 2 times slower than Perl

Ewald van Houte ehoute at nospamzeelandnet.nl
Tue Jul 17 18:26:30 EDT 2001


xucs007 at yahoo.com (Xu, C.S.) wrote in 
news:8f41cfd.0107171238.6ff33b9b at posting.google.com:

> I just compared the speed of Python, Perl, Java, C
> to do simple numerical calculations. Python is the
> slowest: took 150 times of time than C, Perl about
> 60 times, Java is 4.5 times. 
> 
> The source code of Python is: (Other source are
> similar, all use while loop):
> 
> #!/usr/bin/env python
> i = 2.5;
> while i < 1e7:
>         j = 2.5 * 2.5
>         i += 1
> print i, j
> 
> The `time` results on my PII 450M is:
>      python script:     37.93u 0.03s 0:38.03 99.8%
>      perl script:     15.36u 0.03s 0:15.42 99.8%
>      java compiled:     1.07u 0.10s 0:01.20 97.5%
>      C compiled:     0.24u 0.01s 0:00.25 100.0%
> 
> Can anybody explain why Python is always about 1 time
> slower than Perl?
> 
> Regards,

Tried the same with for loop and while loop

def f1():
    	i=2.5
    	while i<1e7:
    	    	j=2.5*2.5
    	    	i+=1
    	print i,j

def f2():
    	i=2.5
    	for i in range(2.5,1e7+1):
    	    	j=2.5*2.5
    	print i,j

def f3():
    	i=2.5
    	for i in xrange(2.5,1e7+1):
    	    	j=2.5*2.5
    	print i,j

Results
f1()     	34.5 seconds
f2()     	47.6 seconds
f3()     	18.8 seconds

some improvement but f3() will still be slower than Perl version (~30%)



More information about the Python-list mailing list