LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2010, 04:43 PM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
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:~#
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-30-2010, 06:22 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 04-30-2010, 06:31 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
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
 
0 members found this post helpful.
Old 04-30-2010, 07:33 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
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.
 
Old 04-30-2010, 11:10 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by smeezekitty View Post
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.
 
Old 04-30-2010, 11:50 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 05-01-2010, 12:21 AM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
perl is faster than python 'cause you can type it with only four keystrokes.
 
Old 05-01-2010, 07:42 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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

Last edited by bigearsbilly; 05-01-2010 at 07:45 AM.
 
Old 05-01-2010, 09:22 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Perl is faster then python!
So what?

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

What is the purpose of this thread?
 
Old 05-03-2010, 06:53 PM   #10
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Quote:
Originally Posted by pixellany View Post
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.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
First perl script, can I make it go faster?.. jhyland87 Programming 10 03-06-2010 07:43 PM
Python or Perl drdroid Programming 29 12-22-2006 09:54 AM
perl vs python yenonn Programming 6 08-01-2006 05:44 AM
Perl or Python JJX Programming 4 04-17-2006 02:42 PM
Python or Perl? Boby Programming 2 06-12-2005 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration