Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which scripting language performs better in vs perl vs python vs ruby? [closed]

Tags:

python

c

ruby

perl

Until now, I've been writing programs in Perl. I decided to give python a try and noticed a few differences. While perl has ARGV, regex, etc. built in, these must be imported in python. I thought this gives python a performance advantage since you're only loading what you really need.

So, I wrote a demo program in each language to test its performance.

Perl

#!/usr/bin/perl

exit(1) if $ARGV[-1] ne 'test';
print "Testing...\n";

my $a = 1.0;
my $i;

for (0 .. 500) { $a+=$a/100; }

printf "Result: %.5f\n", $a;

Python

#!/usr/bin/python

from sys import argv

if argv[-1] != 'test':
   exit(1)

print 'Testing...'

a = 1.0
for i in range(0, 501):
    a+=a/100

print 'Result: %.5f' %a

Ruby

#!/usr/bin/ruby

if ARGV[0] != "test"
 exit(1)
end

print "Testing...\n"
a = 1.0

(0..500).each do a+=a/100 end

printf "Result: %.5f", a

C

#include <stdio.h>
#include <string.h>

int main (int argc, char *argv[]) {

if (strcmp(argv[1], "test") != 0) return(1);

printf("Testing...\n");

double a = 1.0;
int i;

for (i=0; i <= 500; i++)
   a+=a/100;

printf("Result: %.5f\n",a);
return 0;
}

The results are:

Perl

real 0m0.006s
user 0m0.002s
sys 0m0.004s

Python

real 0m0.075s
user 0m0.061s
sys 0m0.013s

Ruby

real 0m0.017s
user 0m0.008s
sys 0m0.008s

C

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

Is my test flawed in some way?

I've read that python is better suited for large programs (See here). Is it gonna outperform perl then? What about their memory usage?

I'm writing a few large applications to be run as daemons on my VPS which has limited amount of RAM so my real goal is to minimize memory usage.

like image 763
perlit Avatar asked Dec 15 '10 06:12

perlit


People also ask

Which language is better Ruby or Python?

Python is generally better for educational use or for people who want to build quick programs rather than work as developers, while Ruby is better for commercial web applications. There are more specific differences when comparing Ruby versus Python, and they have in common that there are many ways to learn both.


1 Answers

There are a few issues...

  1. Your test doesn't accumulate enough runtime, you are probably testing mostly the startup overhead of the interpreter, and not even measuring that very accurately.

  2. I don't care if Perl or Python are 10x faster than Ruby, I want to use what I consider the best language ... the one that I have the most motivation to write in ... the one I think that it's possible to write beautiful code in.

  3. The esr article is quite old and certainly doesn't include Ruby.

like image 138
DigitalRoss Avatar answered Oct 17 '22 00:10

DigitalRoss