Python 2 times slower than Perl

Radovan Garabik spam at melkor.dnp.fmph.uniba.sk
Wed Jul 18 03:48:16 EDT 2001


Xu, C.S. <xucs007 at yahoo.com> 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%


time on my PIII 600:
34.32s user 0.17s system 97% cpu 35.259 total
I am surprised the difference is not bigger, I
thought I have much better processor :-)
(maybe it does not fit into cache? I have 256KB CPU chache,
python surely does not fit there, and if it ends up flushing the
cache all the time, it can explain the lack of improvement)
Could you post your perl script, so that I can make a comparision?

Modified script:

i = 2.5;
while i < 1e7:
    j = 6.25
    i += 1
print i, j
29.20s user 0.08s system 99% cpu 29.490 total

now, i += 1 must coerce 1 to real number. So I modified it like this:
i = 2.5;
while i < 1e7:
    j = 6.25
    i += 1.
print i, j
28.78s user 0.12s system 99% cpu 28.947 total
only a slight improvement
                
using a function:
def f():
        i = 2.5;
        while i < 1e7:
                j = 6.25
                i += 1.
        print i, j
f()

18.53s user 0.02s system 98% cpu 18.872 total
normalized to your timing, this would give
20.5 s for your computer. This is probably more
fair comparision to perl.

-- 
 -----------------------------------------------------------
| Radovan Garabik  http://melkor.dnp.fmph.uniba.sk/~garabik |
| __..--^^^--..__         garabik @ fmph . uniba . sk       |
 -----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!



More information about the Python-list mailing list