loop in python

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Aug 22 23:13:18 CEST 2005

km a écrit :
> Hi all,
> ya i am sorry  i tried with an empty loop first and then one which emits a value as the snippet. I have tested it on my machine and now ... 
> 1) perl (v 5.8) does the job in 0.005 seconds
> 2) but python (v 2.4.1) is horribly slow its 0.61 seconds.
> and using range() instead of xrange() in python snippet, it not better , it takes 0.57 seconds. just test it urself and see.
> what more do i need to accept python is slow when it comes to loops concept ?

#python -> python2.4.1:
for i in xrange(100000):
    print i

real    0m8.997s
user    0m1.355s
sys     0m0.483s

/* C -> gcc -Wall -ansi -pedantic -O3 */
# include <stdio.h>
int main(void)
    int i;
    for (i = 0; i < 100000; i++)
        printf("%d\n", i);
    return 0;

real    0m7.165s
user    0m0.276s
sys     0m0.430s

Well... seems that Python is not *so* slow when compared to C on this 
one. But wait... Could it be possible that we're in fact merely 
benchmarking IO's ?-)


And what about this:

# loops2.pl
for $x (0..100000){}

real    0m0.038s
user    0m0.030s
sys     0m0.002s

# loops2.c
int main(void)
     int i;
     for (i = 0; i < 100000; i++);
     return 0;

real    0m0.002s
user    0m0.001s
sys     0m0.001s

Oh my ! What more do you need to accept Perl is *so awfully slow*.

If you want to keep on trolling on this, I suggest that you "benchmark" 
(lol) a Java, a PHP and a Ruby versions too...

But I don't despair... Chances are that, one day, you understand that 
what is important is what you going on in the loop - not the loop itself.

More information about the Python-list mailing list