Comparing perl and python

Thomas A. Bryan tbryan at python.net
Wed Feb 23 05:13:24 EST 2000


Tim Roberts wrote:
> 
> I've been reading this newsgroup for a couple of weeks, and I'm a bit
> afraid my subject line might label me as a troll, but as a python newbie,
> it is one which interests me.

It's not so bad as long as you don't cross-post to comp.lang.perl.misc.

> Further, although it might be more related to the
> way I learned perl, I find that perl tends to encourage monolithic
> programs, whereas python seems to lend itself more easily to a more
> modularized structure.  These are both Good Things.

I found the same thing.  When I learned Python, I only knew C and Perl.
After programming in Python for a few months, my coding got better not 
just in Python, but also in C and Perl.  Using Python for a year also 
made it much easier to pick up C++ and Java later.

> It's clear that there is a substantial library of modules for python,
> approaching that of perl.  However, I am disturbed by what seems to be a
> dramatic performance difference.  

Search DejaNews for comp.lang.python threads involving performance, Python, 
and Perl.  You'll find quite a few.  Python tends to be slower than Perl 
in the problem domains where Perl really shines.  Most people who start 
such threads are recovering Perl hackers who are writing Python in a 
Perl idiom.  For example, Perl's regular expressions are really fast.
People use them all the time to fairly trivial work.  When rewriting a 
Perl program in Python, I find that I can often replace half of the 
regular expressions by using the string and os.path modules in Python.
That helps speed.  

> I've tried writing some simple test cases in both languages....

If you try to directly translate, you'll almost certainly notice a 
speed hit.  To phrase it poorly ;-)
"Important note: Python programs that aren't very Pythonish tend 
to run much slower than the Pythonish ones. "

It's from a page of mine that I haven't touched recently:
http://starship.python.net/~tbryan/perl2python.html

You might be interested in looking through the one sample that I 
documented.  I'll do others once I have spare time again.

> Now, it's possible this is just because python is version 1.6 and 
> perl is version 5; maybe python hasn't had the attention to 
> optimization that perl has, 

Python has been around for a long time.  The version numbers creep 
forward slowly, but there are huge differences between versions 
1.3 and 1.5.  That said, Python is never going to be optimized 
for input and text processing in the same way that Perl has been
unless some skilled volunteer or company provides a patch that 
implementes such optimizations and doesn't break anything else.

> but I'd like to know if my experiences are unique.  

No.  As I said, look at DejaNews.  Then again, you'll often notice 
that the messages posted with code examples were trimmed and 
rewritten by comp.lang.python posters.  Often performance differences 
can be reduced to a factor of 2 just by ridding the program of 
Perl idioms and avoiding slower Python modules.  Not infrequently, we're 
able to create a Python program that performs the same task as the 
initial Perl program that someone is trying to mimic.  Really, read a 
few threads on DejaNews if you're very concerned.  It should give 
some good ideas.  The topic comes up every few months.

---Tom



More information about the Python-list mailing list