Python 2 times slower than Perl

Radovan Garabik spam at melkor.dnp.fmph.uniba.sk
Thu Jul 19 04:13:20 EDT 2001


Xu, C.S. <xucs007 at yahoo.com> wrote:
 : Just spent a little time to compare 4 stupid again algorithms
 : (changed a little bit from last time) for both python and perl
 : (codes attached) on two systems (SunOS and Linux):
 :        1. while loop, global variables
 :        2. for loop, global variables
 :        3. while loop, local variables (in function)
 :        4. for loop, local variables (in function)
 : I used `time [1234].p[yl]` to get the time info. The user time
 : is as follows:

 : PII 450MHz, Dell Precision 4100 Desktop, SunOS 5.8 i386
 :                #1      #2      #3      #4
 : Python 2.1a1:   5.1     3.1     4.5     2.7
 : Perl 5.005_03:  1.8     1.6     1.7     1.6


 : PIII 1GHz, MTech 8500 Notebook, i386 Linux (Mandrake 8.0, kernal 2.4)
 :                #1      #2      #3      #4
 : Python 2.0:     2.34    1.38    1.54    0.98
 : Perl 5.6.0:     0.95    1.22    0.95    1.21

 : The closest match is on Linux, 0.98 (python#4) vs. 0.95 (perl#1,#3).
 : Python still can't beat Perl, :-(  But I will continue to use Python
 : to do uncritical data analysis, :-)



 : P.S. Codes are:
 : ####################### 1.pl: while loop, global var #####################
 : #!/usr/bin/env perl
 : $i = 0;
 : $j = 0.;
 : while ($i < 1e6) {
 :        $j += $i * 2.5;
 :        $i++;
 : }
 : print $i, ' ', $j, "\n";

 : ####################### 1.py: while loop, global var #####################
 : #!/usr/bin/env python
 : i, j = 0, 0.
 : while i<1e6:
         ^^^^^
1e6 is a real number, this comparision has to coerce i to real number
better comparision would be with i<1000000
for me, it improved the time from 4.65s to 4.14s
and running with python -O cut it down to 3.92s
(now this I do not quite understand... I thought -O
 just removes docstrings, doesn't it?)

-- 
 -----------------------------------------------------------
| 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