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:
Perlreal 0m0.006s
user 0m0.002s sys 0m0.004s
Python
real 0m0.075s
user 0m0.061s sys 0m0.013s
Rub开发者_如何学JAVAy
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.
There are a few issues...
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.
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.
The esr article is quite old and certainly doesn't include Ruby.
There is no general answer to your question about performance and the benchmark does not prove almost anything, performance is too complex issue to be judged by a single test. Modern Perl gives you about as many complexity taming tools as any other decent language and is well suited to writing big programs.
As for the memory efficiency, you could say that this implementation of language X is less memory-hungry than that implementation of language Y. But in practice I think that you’d get much higher variantion based on the way you code and design. And if you are already fluent in one language, you’d probably better use that one and buy extra memory for the money you save by switching to a different language. YMMV.
And after reading the comments: Trying to cut down on memory usage by rewriting to a different language without profiling first is crazy.
No, that's about where I would expect to see those languages in terms of relative performance.
More thoroughly OO languages like Python and Ruby are inherently slower, these two in particular because they are interpreted. (ok python can be compiled, but its still slow)
The reason python would be considered better for larger programs is because it's easier to organize as complexity scales. It takes a lot of rigor to keep a large number of interrelated perl scripts manageable. Especially when you can't read them two weeks later. =o)
Programmer time is as important as cpu time in most cases.
A few issue I can think of:
You operate on floating number. You dont know how it is implemented on each language (which precision ? float ? double ?) It may result in speed/result difference.
Number of count is too small, you should do a nested loop to make the program run more than 10 second.
For example:
for(0:10000) // change variable depending on what your machine
for(0:10000)
// your operation here
精彩评论