Python advocacy

Andrew Dalke dalke at acm.org
Sat Apr 14 04:01:23 EDT 2001


Sheila King asked about perl v. python, for a coworker:
>I have a couple questions about Python:
>1) It's speed in relation to perl...  If figure this will be
>subjective to the task at hand - but hopefully there is an
>average overall performance increase/decrease...

I did some timing tests between different parsers, in my case
for the bioinformatics field.  The results are at
  http://www.biopython.org/wiki/html/BioPython/ParserComparison.html

The most comparable Perl vs. Python comparison is the bioperl
vs. biopython tests, which tests two hand-written parsers for
the SWISS-PROT format.  In both cases the authors are scientists
by training, and the perl code has been used and tweaked by
more people.

The bioperl code takes 30m13s to parse the data set.  The
biopython code takes 28m55s, so ever so slightly faster.  The
biopython code is also doing more work (ie, it does some
validation of the input format, and it builds up a more
complete data structure).

I should also mention the code I've been working on, Martel,
which lets Python parse that data file in 23m29s, or 20%
faster than the perl code.  It's a much better validator to
boot, although it is based on a C extension not available
to Perl.

> I would assume lugging around the OOP methodologies will
>impose overhead which would be pretty much unavoidable...

In this case the Perl code is also building up an OO data
structure.  But Python's is easier to understand and overall
faster than the perl code.

>2) How does it stack up against Perl's regex capabilities, which I rely
>__heavily__ on for massive processing duties...

People use regexps heavily in Perl because Perl promotes heavy
use of regexps.  I rarely these days use regexps because things
like string.split are much more appropriate.

Still, if you want regexp, they are present in the re module.
They also offer a nice advantage over Perl's regexps, named
groups.  For example:

  pat = re.compile("age: (?P<age>\d+)")
  m = pat.search("name: Freddy    age: 10")
  age = m.group("age")

On the other hand, it is a bit more verbose than
  "name: Freddy   age: 10" =~ m/age: (\d+)/;
  $age = $1

>3) How well does it perform auto-vivication of complex data
>structures: e.g.:
> http://www.perl.com/pub/doc/manual/html/pod/perldsc.html

Had to look that link up.  It doesn't explain or even mention
"auto-vivication".  Could you elaborate?

The equivalent of the examples from that Perl page is easy:

@LoH = (
        {
            Lead     => "fred",
            Friend   => "barney",
        },
        {
            Lead     => "george",
            Wife     => "jane",
            Son      => "elroy",
        },
        {
            Lead     => "homer",
            Wife     => "marge",
            Son      => "bart",
        }

LoH = (
        {
         "Lead": "fred",
         "Friend": "barney",
        },
        {
         "Lead": "george",
         "Wife": "jane",
         "Son": "elroy",
        },
        {
         "Lead": "homer",
         "Wife": "marge",
         "Son": "bar",
        }
      )

>**yes, I know that perl's dereferencing syntax is for masochists, but once
>mastered - the sadistic side shines through...

As someone who once worked with them, I can say I had to read
the "Advanced Perl Programming" book to figure out how to use
these data structures, and I could never explain that knowledge
very well to others.

>I have some projects that might fit better into an object oriented way of
>thinking

Programming in Python does not require an OO way of doing things.

# Check for accounts in /etc/passwd missing a password
for line in  open("/etc/passwd").readlines():
   name, pw = string.split(line, ":")[:2]
   if pw = "":
      print name

>however I must be careful of time spent learning 'Yet Another
>Programming Language' since deadlines could quickly slip...

There's no response to this other than (excuse the rudeness) "duh!".
With more elaboration:
  - of course this is true
  - but why did you decided to learn Perl over what you used before?
  - if you don't have time to evaluate any new tools, you will fall
       behind because the world will pass you by

>I don't think anyone can argue that Perl is **extremely** powerful and runs
>the full gamut of simple one-shot scripts - to system level capabilities
via
>syscall()/POSIX interface...

I don't think anyone can argue that Python is **extremely** powerful
and runs the full gamut of simple one-shot scripts - to system level
capabilities - to Java and web applets - to COM interfaces - and
PalmOS support - and even 3D interactive worlds.

                    Andrew
                    dalke at acm.org







More information about the Python-list mailing list