LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl is faster then python! (https://www.linuxquestions.org/questions/programming-9/perl-is-faster-then-python-805236/)

smeezekitty 04-30-2010 04:43 PM

Perl is faster then python!
 
code source: http://www.amk.ca/files/simple/celsius
Ported to perl by me!
Python (hate it):
Code:

import sys,string
# no arguments were given, print a helpful message
if len(sys.argv)==1:
    print 'Usage: celsius temp1 temp2 ...'
    sys.exit(0)

# Loop over the arguments
for i in sys.argv[1:]:
    try:
        fahrenheit=float(string.atoi(i))
    except string.atoi_error:
        print repr(i), "not a numeric value"
    else:
        celsius=(fahrenheit-32)*5.0/9.0
        print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5))

Perl:
Code:

use POSIX;
# no arguments were given, print a helpful message
if($#ARGV == -1){
        print 'Usage: celsius temp1 temp2 ...', "\n";
        exit(0);
}
# Loop over the arguments
foreach (@ARGV){
        local $flag=0;
        local $fahrenheit=$_;
        if(!isdigit($_)){
                $flag=1;
        }
        if($flag == 1) {
                print "\'", $_, "\' ", "not a numeric value\n";
        }
        if($flag == 0){
                local $celsius=($fahrenheit-32)*5.0/9.0;
                printf("%i\260F = %i\260C", $fahrenheit, $celsius+.5);
                print "\n";
        }
}

I had some problems in porting the reason: Missing $ on variables.
Results (Test#1):
Code:

debian:~# time perl celsius.pl 25
25�F = -3�C

real    0m0.098s
user    0m0.064s
sys    0m0.024s
debian:~# time python celsius.py 25
25�F = -3�C

real    0m0.101s
user    0m0.063s
sys    0m0.036s
debian:~#

Results (Test#2):
Code:

debian:~# time python celsius.py 3
3�F = -15�C

real    0m0.097s
user    0m0.052s
sys    0m0.035s
debian:~# time perl celsius.pl 3
3�F = -15�C

real    0m0.091s
user    0m0.052s
sys    0m0.032s
debian:~#

Hello world:
Code:

print "hello world"; #works in python and perl!
Results:
Code:

debian:~# time perl test.py
hello world
real    0m0.026s
user    0m0.012s
sys    0m0.008s
debian:~# time python test.py
hello world

real    0m0.062s
user    0m0.020s
sys    0m0.044s
debian:~#


jlinkels 04-30-2010 06:22 PM

Sorry to spoil your day, but those time intervals are way too small to be accurate. Besides, I am not really impressed by the difference between 0.063s and 0.064s for the Perl and Python version. Let the programs run 10.000 times and measure that time and report back.

jlinkels

smeezekitty 04-30-2010 06:31 PM

I ran it 500 times!
perl:
Code:

real        0m0.577s
user        0m0.056s
sys        0m0.132s

python:
Code:

real        0m0.790s
user        0m0.092s
sys        0m0.136s


Sergei Steshenko 04-30-2010 07:33 PM

Quote:

Originally Posted by smeezekitty (Post 3953437)
I ran it 500 times!
perl:
Code:

real        0m0.577s
user        0m0.056s
sys        0m0.132s

python:
Code:

real        0m0.790s
user        0m0.092s
sys        0m0.136s


I remember trying some simple test and in that case Python was somewhat faster than Perl. I think there certain BKMs how to write fastest possible code under Perl and Python, and the approaches might be different.

Anyway, the main difference between Perl and Python is not speed, but features.

graemef 04-30-2010 11:10 PM

Quote:

Originally Posted by smeezekitty (Post 3953389)
Ported to perl by me!
Python (hate it):

Just some gentle advice. If you want to do a comparison like this then you must be objective about it. Amongst other things that means you need to leave behind any personal prejudices. Often the most compelling research comes from a team trying to prove one hypothesis and end up disproving it.

grail 04-30-2010 11:50 PM

I was also curious as to which version of Perl and Python?

I ask as it may also be necessary to test with Python 3.X (not sure if it would make any difference) to have a fuller picture.

wje_lq 05-01-2010 12:21 AM

perl is faster than python 'cause you can type it with only four keystrokes.

bigearsbilly 05-01-2010 07:42 AM

I did some benchmarks for a fisher yates shuffle,
slurp a file into an array and shuffle it.
for randomizing mp3 files for a USB stick.

Not biased cos I wanted to learn ruby, but was dismayed
how slow it was.

RUBY
$ time ./fisher_yates.rb < don_quixote.txt > 1
0.35s real 0.28s user 0.06s system

PYTHON
$ time ./fisher_yates.py < don_quixote.txt > 1
0.24s real 0.22s user 0.01s system

PERL
$ time ./fisher_yates.pl < don_quixote.txt > 1
0.13s real 0.10s user 0.01s system

But of course C++ knocks 'em for six but takes much more programming time

C++
$ time ./fisheryates < don_quixote.txt > 1
0.06s real 0.04s user 0.01s system

pixellany 05-01-2010 09:22 AM

Quote:

Perl is faster then python!
So what?

Quote:

Python (hate it):
Then don't use it!!

What is the purpose of this thread?

exvor 05-03-2010 06:53 PM

Quote:

Originally Posted by pixellany (Post 3953817)
So what?

Then don't use it!!

What is the purpose of this thread?

I think smeeze is just trying to stir the poop kettle and start a flame war :P.


All times are GMT -5. The time now is 12:15 AM.