Speed Comparison Perl Python & C

From: Bart Nessux (bart_nessux_at_hotmail.com)
Date: 02/29/04


Date: Sun, 29 Feb 2004 12:44:51 -0500

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?



Relevant Pages

  • Re: Why is java considered a language for "web" or "internet" programming?
    ... I don't remember reading exact things about Perl interpreter, ... load and parse bytecodes. ... I think, integer in Perl or Python or other scripting languages is some structure, with at the least field for type and value, and pointer to this structure you hold in 'i' variable. ... const int NNUM = 1000000; ...
    (comp.lang.java.help)
  • Re: Python or PHP?
    ... If it's one of the things for which Python ... every language here and there more ways to do something. ... you make Perl more complicated than it is:-D. ... Not the programmer. ...
    (comp.lang.python)
  • Recommend an E-book Meeting the Following Criteria (Newbie, Long)
    ... I know several programming languages namely Java, Perl and C in this order ... Now I'm wondering which Python book I should get as there are so many out ... I'd like to mostly concentrate on language features but some pointers ...
    (comp.lang.python)
  • Re: Choosing Perl/Python for my particular niche
    ... >> For most general purpose tasks, I reach for Python first. ... > syntax and perhaps my lack of bottomless commitment (knowing Perl would not be ... verilog) so that it can be used for another e.g. verilator, ... using a standalone simulator to simulate the original verilog. ...
    (comp.lang.perl.misc)
  • Re: Choosing Perl/Python for my particular niche
    ... >> For most general purpose tasks, I reach for Python first. ... > syntax and perhaps my lack of bottomless commitment (knowing Perl would not be ... verilog) so that it can be used for another e.g. verilator, ... using a standalone simulator to simulate the original verilog. ...
    (comp.lang.python)