python vs. perl speed comparisons 
Author Message
 python vs. perl speed comparisons

I've been programming in perl for several years and am just now learning
python.  I prefer python's syntax and structure, but one thing I'm curious
about is how python and perl compare in terms of speed.  I realize that
this is going to be extremely task-specific, but I wonder if anyone has
compared them from the standpoint of e.g. simple shell tools or text
editing utilities.  If so, I'd like to know how much faster (if at all)
perl is relative to python.

--
------------------------------------------------------------

Department of Computation and Neural Systems, Caltech 216-76
------------------------------------------------------------



Sun, 18 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons


Quote:
>                                                [...] one thing I'm curious
> about is how python and perl compare in terms of speed.

Well, this summer I wrote a small Python program to parse the
debug logs from sendmail to gather statistics about what domains
sent mail to us and to what domains we sent mail.  I then rewrote
it as a perl script.  The perl version was 10 times faster.

Also, the following simple test programs show that Perl is about
2.5 times as fast as Python on integer addition and comparisons.

        i = 0                           $i = 0;
        while i < 250000:            while ($i < 250000) {
                i = i + 1                       $i = $i + 1;
                                        }

And if I use the ++ operator in Perl, I get a speedup with a
factor of three, making it seven or eight times as fast as the
Perl program.

Much as I like Python, it hasn't yet impressed me with its speed.

--
Thomas Bellman,   Lysator Academic Computer Club,   Link?ping University
"The one who says it cannot be done should     !  Sweden ; +46-13 177780



Sun, 18 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

hola.

Quote:
Michael Christopher Vanier writes:

 > I've been programming in perl for several years and am just now learning
 > python.  I prefer python's syntax and structure, but one thing I'm curious
 > about is how python and perl compare in terms of speed.  I realize that
 > this is going to be extremely task-specific, but I wonder if anyone has
 > compared them from the standpoint of e.g. simple shell tools or text
 > editing utilities.  If so, I'd like to know how much faster (if at all)
 > perl is relative to python.

Well, I can give you my point-of-view.

Perl: Whenever I need to do string manipulation or file translation via
      work with regular expressions, I use Perl.  I find the
      development time to be faster.  Perl is also *very* fast at these
      tasks.  Much faster than Python.

Python: WhenverI want to do network programming or use real data
        objects, I use Python.  I find the development time to be much
        faster.  Python certainly seems fast at these tasks, although I
        have no speed comparisons.

I have yet to write a simple shell tool or a text editing utility in
which raw speed was the most critical feature.  I choose my development
language for ease of development and maintenance.  I have not had any
performance problems with Python (or Perl).

--
.....................................................................
.   happy daze            .   I don't have a solution,              .
.        -tim O           .       but I admire the problem.         .
.....................................................................



Sun, 18 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

Quote:



> > >                    [...] one thing I'm curious
> > > about is how python and perl compare in terms of speed.

> > Well, this summer I wrote a small Python program to parse the
> > debug logs from sendmail to gather statistics about what domains
> > sent mail to us and to what domains we sent mail.  I then rewrote
> > it as a perl script.  The perl version was 10 times faster.

[...]

>         I'm not an expert on programming but I do have the common sense to know
> that
>  language that compiles before it runs will easily beat a language that
> is interpreted if the benchmark test's code is short and loopy.
>         But if the benchmark test code was linear and big then there wouldn't
> be much of a difference, or maybe the interpreted language would win,
> here are the stats

[...]

Python does compile its code...

The key difference is that Python is a damn nice language now and could become
a fast language later. Perl will always be ugly.

-Tony Lownds



Sun, 18 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.python,

:
:       I'm not an expert on programming but I do have the common sense to know
BAD WRAP>that
: language that compiles before it runs will easily beat a language that
:is interpreted if the benchmark test's code is short and loopy.
:       But if the benchmark test code was linear and big then there wouldn't
:be much of a difference, or maybe the interpreted language would win,
:here are the stats
:
:short loopy code:
:
:       perl:
:               easy compile- fast
:               run     time-   short (got a lot out of its initial investment)
:
:       python:
:               compile time- zero
:               interpretation time- slower
:
:
:linear and long code:
:      
:       perl:
:               long compile time- slow
:               run time- fast
:
:       python:
:               no compile time
:               interpretation time- slower, but got an early start.

You are confused if you think that in their traditional modes perl and python
act differently.  Both are "interpilers", or byte-compilers, or whatever
you want to call them.  Both have a parse pass which compiles your program
into internal format.  This is very separate from the execution pass over
the byte-code/parse-trees so generated.  If you save just the byte-code,
you sae the compile pass each time.

On the other hand, one *can* compile Perl into C and let gcc -O4 do its
dirtiest tricks, but that's way different.  And I can assure you with
90% certainty that the original poster was not using the perl compiler,
just the standard /usr/bin/perl interpiler.

--tom
--

"Espousing the eponymous /cgi-bin/perl.exe?FMH.pl execution model is like
reading a suicide note -- three days too late."



Mon, 19 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

Quote:


> >                                                [...] one thing I'm curious
> > about is how python and perl compare in terms of speed.

> Well, this summer I wrote a small Python program to parse the
> debug logs from sendmail to gather statistics about what domains
> sent mail to us and to what domains we sent mail.  I then rewrote
> it as a perl script.  The perl version was 10 times faster.

> Also, the following simple test programs show that Perl is about
> 2.5 times as fast as Python on integer addition and comparisons.

>         i = 0                           $i = 0;
>         while i < 250000:               while ($i < 250000) {
>                 i = i + 1                       $i = $i + 1;
>                                         }

> And if I use the ++ operator in Perl, I get a speedup with a
> factor of three, making it seven or eight times as fast as the
> Perl program.

> Much as I like Python, it hasn't yet impressed me with its speed.

        I'm not an expert on programming but I do have the common sense to know
that
 language that compiles before it runs will easily beat a language that
is interpreted if the benchmark test's code is short and loopy.
        But if the benchmark test code was linear and big then there wouldn't
be much of a difference, or maybe the interpreted language would win,
here are the stats

short loopy code:

        perl:
                easy compile- fast
                run     time-   short (got a lot out of its initial investment)

        python:
                compile time- zero
                interpretation time- slower

linear and long code:

        perl:
                long compile time- slow
                run time- fast

        python:
                no compile time
                interpretation time- slower, but got an early start.



Mon, 19 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

Quote:
> The key difference is that Python is a damn nice language now and could become
> a fast language later. Perl will always be ugly.

You mean you don't like trying to remember the difference between $[ $]
$^ $% $' $" or whatever in Perl??  come on!  it's soooo readable!!  much
better than sys.version or some ugly trash like that..  :)  __sarcasm
mode OFF__
--



Mon, 19 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

Quote:


...
>> Also, the following simple test programs show that Perl is about
>> 2.5 times as fast as Python on integer addition and comparisons.

>>         i = 0                           $i = 0;
>>         while i < 250000:               while ($i < 250000) {
>>                 i = i + 1                       $i = $i + 1;
>>                                         }

>> And if I use the ++ operator in Perl, I get a speedup with a
>> factor of three, making it seven or eight times as fast as the
>> Perl program.

>> Much as I like Python, it hasn't yet impressed me with its speed.

>    I'm not an expert on programming but I do have the common sense to know
> that
>  language that compiles before it runs will easily beat a language that
> is interpreted if the benchmark test's code is short and loopy.

And *I* have the common sense to know that it doesn't matter if
Python interprets my code, byte compiles it or compiles it to
native machine language, as long as it is slow.  And it doesn't
matter *how* Perl achieves its speed, as long as it's fast.

If I am concerned about speed, it *is* fair to compare a compiler
to an interpreter, since the compiler almost certainly *is*
faster than the interpreter.

And as others have pointed out, both Python and Perl compiles
their programs to byte code before executing it.  It's just that
Perl has a faster implementation.  But that really is beside the
point.

However, the small benchmarks I wrote above, only give *one*
datapoint in the comparison between the speed of Perl vs Python.
Not all programs are so integer intensive.  Most of my Python
programs do database accesses, and are limited by the speed of
the database engine, not by the Python interpreter.

--
Thomas Bellman,   Lysator Academic Computer Club,   Link?ping University
"Too much of a good thing is WONDERFUL."       !  Sweden ; +46-13 177780



Mon, 19 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

Apparently, I was a victim of the perl-compiles-all-the-way myth that
been going around, (I've even seen in it print).  I've since done my
research and confirmed my error.   I believe this myth has gained
acceptance because perl is so fast.  Does anybody know why two
interpreted languages can differ so much in speed.  Does perl compile
further down to machine language than python does?

No to change the subject too much, but I'm about to start a project, and
for this project I wan't to be able to extract a substring found with a
regular expression.  I believe that python and its modules have this
capability, but can perl do the same thing?  I would rather use perl
because its so much faster (sorry) and because this is only going to be
a small script.

Quote:

>=20

>=20

> ...
> >> Also, the following simple test programs show that Perl is about
> >> 2.5 times as fast as Python on integer addition and comparisons.

> >>         i =3D 0                           $i =3D 0;
> >>         while i < 250000:               while ($i < 250000) {
> >>                 i =3D i + 1                       $i =3D $i + 1;
> >>                                         }

> >> And if I use the ++ operator in Perl, I get a speedup with a
> >> factor of three, making it seven or eight times as fast as the
> >> Perl program.

> >> Much as I like Python, it hasn't yet impressed me with its speed.

>=20
> >       I'm not an expert on programming but I do have the common sense=
 to know
> > that
> >  language that compiles before it runs will easily beat a language th=
at
> > is interpreted if the benchmark test's code is short and loopy.
>=20
> And *I* have the common sense to know that it doesn't matter if
> Python interprets my code, byte compiles it or compiles it to
> native machine language, as long as it is slow.  And it doesn't
> matter *how* Perl achieves its speed, as long as it's fast.
>=20
> If I am concerned about speed, it *is* fair to compare a compiler
> to an interpreter, since the compiler almost certainly *is*
> faster than the interpreter.
>=20
> And as others have pointed out, both Python and Perl compiles
> their programs to byte code before executing it.  It's just that
> Perl has a faster implementation.  But that really is beside the
> point.
>=20
> However, the small benchmarks I wrote above, only give *one*
> datapoint in the comparison between the speed of Perl vs Python.
> Not all programs are so integer intensive.  Most of my Python
> programs do database accesses, and are limited by the speed of
> the database engine, not by the Python interpreter.
>=20
> --
> Thomas Bellman,   Lysator Academic Computer Club,   Link=F6ping Univers=
ity
> "Too much of a good thing is WONDERFUL."       !  Sweden ; +46-13 17778=
0


e


Mon, 19 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

: Well, this summer I wrote a small Python program
: to parse the debug logs from sendmail to gather
: statistics about what domains sent mail to us and
: to what domains we sent mail.  I then rewrote
: it as a perl script.  The perl version was 10 times
: faster.
[...]
: Much as I like Python, it hasn't yet impressed me with its speed.

 Be sure to wrap your mega-processing code within a function... using
globals slows things down considerably because of (I think) the way Python
deals with namespace searching.  Also, for your sendmail log parser, did
you try the regex.compile method with regex.group() methods to get the
data out?  That may also speed things up.  For text munging, though, there
is no denying that Perl just flies.  If you aren't planning to reuse or
update that code, I'd leave it as Perl for now.

 Initially, I was also a bit put off by Python's speed hit.  Based on some
benchmarks my co-workers and I did last year, we figured out that Perl is
around twice as fast as Python (the tests also included array indexing, FP
calculation, system calls and file processing).  Since then, I've come to
a slightly different interpretation of the results:

 Perl code requires 10 times more effort to maintain than Python code
requires.

 That may sound hypothetical, but, based on experience maintaining a large
system of scripts written in Perl, I think it is a pretty realistic
number.  Perl is an awk/sed/csh replacement that is now used in places
that demand more than it offers.  While it is totally powerful for throw
away scripts, and for that I still use it often, Perl is not even a
contender when compared to Python (or just about any other language) in
terms of long-term code maintenance.  The main reason is because Perl is
too hard to read and deal with after you've written it.... even if it's
your own code.  The money market page in the WSJ has less symbols than a
line of Perl code.  'Nuff said.

 ... okay, I've had my Perl rant time for today.

 - Chris




Tue, 20 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

        >snip<
: No to change the subject too much, but I'm about to start a project, and
: for this project I wan't to be able to extract a substring found with a
: regular expression.  I believe that python and its modules have this
: capability, but can perl do the same thing?  I would rather use perl
: because its so much faster (sorry) and because this is only going to be
: a small script.

        Do you meen something like:

                $MyString = "string my is This";
                $MyString =~ /^(.*) (.*) (.*) (.*)/;
                print "$4 $3 $2 $1\n";

        Which prints out:
                This is my string

        In other words, just use () to grab the part you want to save by
        itself.

        Being *vary* new to Python (but openminded), can someone please show
        me what this would look like in Python?
______
     /  Spelling mistakes?  Their couldn't be. -My modem is error correcting.
    /

  /      ___    __  o   __
 /      /___\ |/  \ | |/  \                 http://www.best.com/~zenin/
/_______\___  |   | | |   |_______________________________________________



Wed, 21 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

:       Do you meen something like:
:
:               $MyString = "string my is This";
:               $MyString =~ /^(.*) (.*) (.*) (.*)/;
:               print "$4 $3 $2 $1\n";
:
:       Which prints out:
:               This is my string
:
:       Being *very* new to Python (but openminded), can someone please show
:       me what this would look like in Python?

The only real difference is that Python's regular expressions are
Emacs-style, and it has a bit more objective coding overhead.  For simple
examples and throwaway code, Perl still has the upper hand for most regex
stuff ... for long, complex scripts, Python's objective style of writing
this code is easier to deal with, in my opinion.  Some people have worked
on offering Perl's more powerful regex syntax style in Python, but I
haven't looked into it yet.

import regex

mypat = regex.compile("^\(.*\) \(.*\) \(.*\) \(.*\)")
mypat.match("string my is This")
print "%s %s %s %s\n" % mypat.group(4, 3, 2, 1)

 - Chris



Wed, 21 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

        >snip<
        Ok, we are now really getting of the original subject, but then
        that's what usenet is great at. ;)

: import regex
        Hm, I take it to meen that Python doesn't have to load regex code
        unless the program actualy needs is?  It seems like this style
        could speed things up once Python is optimized a bit more. -Many of
        my perl programs don't use regex, or at least nothing that couldn't
        be done almost as easyly with a substr(), not to mention a ton of
        other native perl functions that I'd rather not have in the main
        code if I didn't request them. -eg; I'd like to see much more of
        Perl implemented as modules.

: mypat = regex.compile("^\(.*\) \(.*\) \(.*\) \(.*\)")
        Now this IS ugly... :-/  This regex syntax alone would keep me from
        trying any regex that was even remotely interesting. -Some think
        Perl is hard to read but man could that get really ugly fast, not to
        mention buggy.  This is one of the reason's I can't stand using sed
        for anything complex (I've got sed aliased to "perl -ep"
        anyway. <grin>).

        Is there anyway to embed comments into a Python regex?

: mypat.match("string my is This")
        Kind of nice, compiling the regex once into an object so as not to
        need to type it out again.  However, I still have yet to see an
        advantage (syntax wise) over perl in this case:

                $MyPat = '^(.*) (.*) (.*) (.*)';
                "string my is This" =~ /$MyPat/;

        Same style/concept, different language.

: print "%s %s %s %s\n" % mypat.group(4, 3, 2, 1)
        Is there not interpolation in Python?  There *must* be a cleaner
        way to do this last line..  Hell, even printf is easier to read
        then the above... :-/

        However, does this meen if I used two such regex objects, that each
        would store it's indexes (mypat.group(1, 2, 3, etc)) in it's own
        space? -Perl only gives you $1, $2, etc and they are gobal, so the
        next search is likely to blow them away.
        eg: Would this work?

                import regex            

                MyPatOne = regex.compile("^\(.*\) \(.*\)");
                MyPatTwo = regex.compile("\(.*\) \(.*\)$");
                MyVar    = "string my is This";

                MyPatOne.match(MyVar);
                MyPatTwo.match(MyVar);

                print "%s %s "  % MyPatOne.group(2, 1);
                print "%s %s\n" % MyPatTwo.group(2, 1);

        Hmm, is there a way in Python to toss them all into an array easily?
        Or is mypat.group already an array, just indexed strangly?
______
     /  Spelling mistakes?  Their couldn't be. -My modem is error correcting.
    /

  /      ___    __  o   __
 /      /___\ |/  \ | |/  \                 http://www.best.com/~zenin/
/_______\___  |   | | |   |_______________________________________________



Wed, 21 Apr 1999 03:00:00 GMT  
 python vs. perl speed comparisons

        >snip<
:  I think you mean
:    $MyPat = "^" .
:                   "(.*) ".  
:                   "(.*) ".   # etc...
:  One problem with Perl is exactly what was demonstrated here... it is a
: lot easier to make mistakes trying to do simple things.
        Sorry, but my Perl was correct.  Your code is not only missing a
        semicolon, but also now has a trailing concatenation opperator. -Not
        to mention your indentation is funky. :-/  Remember, Perl, like
        English and most other languages, would like some form of period
        (semicolon) to mark the end of a sentance (statement).
______
     /  Spelling mistakes?  Their couldn't be. -My modem is error correcting.
    /

  /      ___    __  o   __
 /      /___\ |/  \ | |/  \                 http://www.best.com/~zenin/
/_______\___  |   | | |   |_______________________________________________



Fri, 23 Apr 1999 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Perl speed vs. Python speed

2. Speed comparison: Python-Perl-Java-Smalltalk

3. Speed comparison: Python-Perl-Java-Smalltalk

4. Speed of Python vs. Perl

5. Speed problems with Python vs. Perl

6. Yet another Python vs. Perl speed issue/question

7. Speed of Python vs. Perl

8. Python vs Perl: benchmarking for speed?

9. Perl vs TCL (was: Execution speed of Perl?)

10. Python Binding [Was: Re: PYTHON VS. PERL VS. TCL ]

11. LONG vs ULONG - Speed Comparisons

12. Forth vs Python vs Perl

 

 
Powered by phpBB® Forum Software