Speed Comparison Perl Python & C

ziaran nir1408 at ziaran.org
Sun Feb 29 14:05:32 EST 2004


Bart Nessux wrote:
> Just fooling around this weekend. Wrote and timed programs in C, Perl and
> Python. Each Program counts to 1,000,000 and prints each number to the
> console as it counts. I was a bit surprised. I'm not an expert C or Perl
> programming expery, I'm most familiar with Python, but can use the others
> as well.
> 
> Here are my results:
> 
> C = 23 seconds
> Python = 26.5 seconds
> Perl = 34.5 seconds
> 
> Here are the programs:
> 
> -------------------------
> #The C version:
> -------------------------
> 
> #include <stdio.h>
> 
> int main(void)
> {
> int x = 0;
> while (x < 1000000) {
>    printf("%d \n", x++);
>    }
> }
> 
> -------------------------
> #The Python version:
> -------------------------
> 
> #!/usr/bin/python
> 
> x = 0
> while x < 1000000:
>    x = x + 1
>    print x
> 
> -------------------------
> #The Perl version:
> -------------------------
> 
> #!/usr/bin/perl -Tw
> 
> use strict;
> 
> my $x = 0;
> while($x < 1000000) {
>    print $x++, "\n";
>    }
> 
> What do you guys think of this? I don't know enough about Perl & C, and
> perhaps Python, to know if this was indeed a fair test. I thought C would
> do this much faster than it did. Any ideas? 
> 
> 
> 
> 

Last time I checked doing simple prime number searching, C++ was 30 
times faster than Python.





More information about the Python-list mailing list