ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.