Python 2 times slower than Perl
James Logajan
JamesL at Lugoj.Com
Tue Jul 17 20:34:42 EDT 2001
Ewald van Houte wrote:
[ Parts elided for brevity. ]
> xucs007 at yahoo.com (Xu, C.S.) wrote
> > #!/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%
>
> 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 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
> f3() 18.8 seconds
>
> some improvement but f3() will still be slower than Perl version (~30%)
You forgot to normalize your result with his by using the results of f1():
>>> 15.36/(18.8*(34.5/37.93))
0.898249768733
Only about 10% slower.
More information about the Python-list
mailing list