Better than Python?
Perl versus Python: Crunch Time
By Shane Bruce 28th of August 2019
I’ve years’ experience in programming scripting languages since the Net was new, a
veritable Antediluvian. Now, I’d take a decade or so walk-about to clear my mind and
continue peak health, so I hadn’t been the desk jockey that I once was in sometime.
Occasionally I’d apply for a job and have some headhunter ask me if I’d such-and-such
experience, to which my reply “I’m a Physicist, I’ve learned a dozen or more computer
languages they are simple I’m sure whatever it is you have I can more than competently
fulfill.” Of course, those head-hunters didn’t’ know a computer code from noise, but
none had said, “Oh your right, proven yourself with degrees in the hardest education
there is, of course you can compete against high-school self-educated. Not to mention
your decade or so developing these technologies, maybe you’re overqualified!”
How ever there was one language that I felt a little bit shy from and that was the much
and hugely lauded Python. Like it was the most powerful, hit-it-and-get-it, data-scientist
worthy language since I’d invented charts for web-traffic. I was slightly intimidated.
Now, unlike most, since I know I’m all that behind a computer board. I don’t often
simply go out to learn something just because ‘everyone’ is talking about it. Why? Not
because I’m lazy as a good Perl programmer could be, nor even the required Hubris to
quote Larry Wall, Perl’s author. But because I know anything to do with computers is
going to be outdates and obsolete in 6-months to 3 years, no matter what it is. So, in
pursuit of some efficiency, I learn as I need to for whatever purpose I have and
occasionally to get paid.
But then I get this email asking me to come into an interview for a data-scientist
position. And it requires all my normal well-used tool kit but also MATLAB and... you got
it Python. Now I’ve only used MATLAB during my many intermitted times as a student.
If I get bored, I go back to college to see what’s new in the world for a year or so. It’s
what I call fun.
But now, I want to know Python. So, I look at it for the first time, and realize it’s like
someone tried to make Perl easier, with lots of shortcuts. Heck, I’d been doing that for
YEARS, I called them my Perl libraries. So, I was ready to take down a bear in what I
thought was some completely new language and realize I know it already, leave out
some semi-colons, don’t do all the hard work like declaring variable or worry about
syntax and Bing! Python. I do think that the loops only formed by indention are
1
innovative. I’ve always worked in University Sector research, so all my code is painfully
exact in its formatting and programming etiquette. So, Python is easier to me than I
really had ever realized.
My possible disappointment of finding something really challenging and completely
unprecedented and incredible nascent technology that I’d somehow been impressed
that Python was more than mollified by the realization that I’d found EASY STREET, and
a lucrative EASY STREET, also known as the language that was named after some Monty
Python movies. I had really though it was after a snake.
So, I got down to the speed tutorials written by the creators of Python and while I liked
the ease of writing it, and the speed as it’s included every short cut a lazy programmer
would want, I couldn’t stop comparing it to Perl. So that’s what I did, my first Phyton
program, written in under an hour, I went ahead and wrote another Perl script to
compare the two head on side-by-side. Here are the results:
SPOILER ALERT
O.K. Honestly, I decided to this as my first program was to calculate the Fibonacci
Sequence as directed by the official Python website tutorial. But that it crapped out with
a memory flow problem at calculating the sequence to 40,000,000 (Forty Million).
What Geek could resist knowing the Fibonacci Sequence into the Millions, right?
I knew Perl could handle it, so I wrote it in Perl. Now also honestly, there may be some
or different method to make it work, but HEY I’m using the official website,
this is supposed to be their strongest step forward.
But to make it interesting I also made it determine if each value is a prime number or
not and track the elapsed time it takes to calculate each sequence.
THE TEST
This takes place on an AWS (AMAZON WEB SERVER) LINUX machine in the AWS Cloud.
Those same computers that Amazon, the government, research and everything else is
based on if on the Cloud. It’s an EC2 “Instance” by AWS Terminology. With Python 2.7
and Perl 5 version 16.
The EC2 instance is an Amazon Linux AMI version 2018.03 rhel fedora
It’s the cheapest Linux server AWS will virtually hosts, I am on a budget using my own
money.
Here are my two programs so you’ve a readymade empirical test you can run on any
machine. If the machine was busy, or I ran them simultaneously Python is 20% slower.
2
test.python (NOTE BONO however that the Python script is much shorter)
#!/usr/bin/python
import time
print("Hello")
x=int(input("Limit to Fibonacci:"))
count1 = 0
count2 = 0
a, b = 0, 1
start = time.time()
while a < x:
count2 +=1
print a,
prime = True
for n in range (2,a):
if a % n == 0:
print ': equals', n, '*', a//n,
prime = False
Break
if prime and a > 1:
print ': Prime number',
count1+=1
a, b = b, a+b
Print
Print
etime = (time.time()-start)*1000
print "Elasped time:%8.3f" % etime,
print "MillSec"
print 'For', x, 'there are', count2, "values"
print 'in the Fibonacci Sequence,'
print 'of which', count1, 'are Prime Numbers.'
Print
Print
3
test.perl
#!/usr/bin/perl
# Written by Shane Bruce 28th of Aug 2019
# as a speed and capability comparison
# of Perl v Python : test.perl and test.python
#
use strict;
use warnings;
use Time::HiRes qw[gettimeofday tv_interval];
print "Hello \nLimit to Fibonacci:";
my $x = ;
chomp $x;
print "\n";
my $prime;
my $count1 = 0;
my $count2 = 0;
my $a = 0;
my $b = 1;
my $c;
my $time1=[gettimeofday()];
while ($a < $x){
$count2++;
print $a;
$prime = 1;
for (my $n=2; $n<=$a; $n++){
if ($a%$n==0 && $a>2 && $a!=$n){
print " : equals ",$n, "*", $a/$n;
$prime = 0;
last;
}
}
if ($prime==1 && $a>1){
print ": Prime number";
$count1++;
}
$c = $a;
$a = $b;
$b = $b+$c;
print "\n";
}
my $time2=[gettimeofday()];
4
my $msec1 = tv_interval($time1)*1000;
my $msec2 = tv_interval($time2)*1000;
print "\nElapsed time: ", $msec1-$msec2, " MillSec.\n";
print "For ", $x;
print " there are ", $count2, " values";
print "\nin the Fibonacci Sequence;";
print "\nof which ", $count1, " are Prime Numbers. \n\n\n";
5
Output for a limit of 999999
python test.python
Hello
Limit to Fibonacci:- : Prime number
3 : Prime number
5 : Prime number
8 : equals 2 * 4
13 : Prime number
21 : equals 3 * 7
34 : equals 2 * 17
55 : equals 5 * 11
89 : Prime number
144 : equals 2 * 72
233 : Prime number
377 : equals 13 * 29
610 : equals 2 * 305
987 : equals 3 *- : Prime number
2584 : equals 2 *- : equals 37 *- : equals 3 *- : equals 2 *- : equals 89 *- : Prime number
46368 : equals 2 *- : equals 5 *- : equals 233 *- : equals 2 *- : equals 3 *- : Prime number
832040 : equals 2 * 416020
test.perl
Hello
Limit to Fibonacci:-: Prime number
3: Prime number
5: Prime number
8 : equals 2*4
13: Prime number
21 : equals 3*7
34 : equals 2*17
55 : equals 5*11
89: Prime number
144 : equals 2*72
233: Prime number
377 : equals 13*29
610 : equals 2*305
987 : equals 3*-: Prime number
2584 : equals 2*- : equals 37*- : equals 3*- : equals 2*- : equals 89*-: Prime number
46368 : equals 2*- : equals 5*- : equals 233*- : equals 2*- : equals 3*-: Prime number
832040 : equals 2*416020
Elapsed time: 100.779 MillSec
For 999999 there are 31 values
in the Fibonacci Sequence,
of which 9 are Prime Numbers.
Elapsed time: 69.012 MillSec.
For 999999 there are 31 values
in the Fibonacci Sequence;
of which 9 are Prime Numbers.
6
So, the Perl is OBVIOUSLY FASTER, and a more stable coding language. Now this is to be
expected. It’s a mature language which has been refined and become faster and more stable
with it’s of it’s 5th General Perl and subsequent 15 versions. The hype from Python was mindboggling, it’s not anywhere near the standard that a professional development language should
be. My only guess as to why it’s lauded, it’s cheap on keystrokes and easy to learn. It should
NOT be topping the lists of requisites by development companies. But here is the closing
argument: Python breaks in this simple program when the limit is 40,000,000 (Forty Million).
It’s lauded as a BIG DATA language; most programming languages handle the number
2,247,483,647 (BIG INT) around Two Billion. Python simply is collage of quick steps to a weaker
foundation backing the language. Unless I’m missing some ‘switch’, but I’ve the impression that
Python is supposed to be the language that doesn’t need the fine-tuning to write a program.
But first Notice how much FASTER Perl is, which in BIG DATA where it counts most.
test.python
Elapsed time:- MillSec
For- there are 38 values
in the Fibonacci Sequence,
of which 9 are Prime Numbers.
test.perl
Elapsed time: 68.779 MillSec.
For- there are 38 values
in the Fibonacci Sequence;
of which 9 are Prime Numbers.
And finally, at 40,000,000 (Forty Million) Python Breaks
test.python (40,000,000)
Traceback (most recent call last):
File "test.python", line 26, in
for n in range (2,a):
MemoryError
test.perl
Elapsed time: 69.151 MillSec.
For- there are 39 values
in the Fibonacci Sequence;
of which 9 are Prime Numbers
Now Perl I ran at a limit of 100,000,000,000 (One Hundred Billion). It did finally start slowing
down in the 2 Billion range on the prime number as it had to run a Modulus (division with no
remainder) all 2 Billion times. So, some seconds appear for the first time in Perl test.
test.perl
Elapsed time:- MillSec.
For- there are 60 values
in the Fibonacci Sequence;
of which 11 are Prime Numbers.
It’s a good program because the size of the numbers rapidly geometrically increases but the
output of the Fibonacci is only the a few dozen values. Perl’s overwhelming Python in speed of
numerical calculations and capacity. But now I like Python, it’s some easy code, but I thought
I’d be able to brag, but only to those who are programming language illiterate. Perl gives a
better shake, though once too thought so simple as to be lame by C++ or Assembly in turn
7