Python vs Perl: benchmarking for speed? 
Author Message
 Python vs Perl: benchmarking for speed?

Has there been any benchmarking of Perl vs Python? If I wanted to write some
code that could be dome equally as well in either, but of course I wanted it
to run as fast as possible, which might I choose?

I'm not trying to start a war, but get some reasoned answers: is one better
than the other for pattern matching, or arithmetic, or communicating with
sockets, etc.

My impression is that Perl might be faster, because it gets "compiled",
whereas python is interpreted, but that's a pretty shallow impression, no
doubt.
--
PJDM
Peter Mayne
Digital Equipment Corporation (Australia)
Canberra, ACT
----
These are my opinions, and have nothing to do with Digital.
The truth is out there, but not necessarily in here.



Wed, 13 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

   My impression is that Perl might be faster, because it gets "compiled",
   whereas Python is interpreted, but that's a pretty shallow impression, no
   doubt.

In that sense Python is also compiled ...

I think speed is rather a question of how a program is written -
if one writes in one language using the idioms of another, the
result is likely to be slow. There was some discussion in this
newsgroup about speed issues, but maybe some competent person
could summarise things - like constructs to be avoided and guidelines
for speeding things up. There is a section in the FAQ with an
appropriately sounding title, but it doesn't contain that much in
terms of concrete tips. Things that come to mind are reading
files all at once rather than line by line, or defining functions
as class members rather that at top level in a module. I'm pretty
sure that more experienced users have a lot more of such hints to
share ...

MS

--
Michal S.



Wed, 13 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

Quote:
>anyway, do you think that the main problem is that people don'[t
>understand what it takes to create maintainable perl code coupled with
>them not understanding about the cpan archive, or is it something else?
>what modules would you prefer were bundled with perl which are not?

The biggest problem I have with perl maintainability (and I have 3
largish perl scripts I wrote and rely on daily) is its syntax is not
graceful. One statement can hold a barrel full of monkeys.

As far as libraries go, I didn't even know about cpan (still don't
know where its at), so yes that could be problem.

Now aday's, when I embark on a script project, I evaluate the need for
regular expressions -- if I need them heavily, I use perl, else I go
with python or even csh if its simple...

rjf

--
"...fame is a cancer and ego its seed...",Toad the Wet Sprocket



Fri, 15 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

Quote:


>   My impression is that Perl might be faster, because it gets "compiled",
>   whereas Python is interpreted, but that's a pretty shallow impression, no
>   doubt.

>In that sense Python is also compiled ...

I think that you should use the language that you feel
best fits the work. There is no reason why you should
only have one size or type of hammer. And a bigger
hammer does not infer it is better for the task at hand.

I pick from the languages available when implementing
software. It was not too long ago when DCL scripts
and VAX-11 C where the only choices. In the end DCL
won out as the core vehicle because of a whole
set of reasons (none personal).

I have come to love Perl5, but then I love C, Ada, Prolog,
LISP, BCPL, Algol and a whole range of other languages.

What I use for work is never decided by personal preference.

Language wars are a waste of time...

Jacqui
--

echo '$a' | adb
Machine Always Crashes, If Not The Operating System Hangs



Fri, 15 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

I agree with your comments below, especially the "language wars are a waste of
time" one.

However, what if (for instance) I'm writing a tool that involves lots of
pattern matching. I could equally well write this tool in (for instance again)
Perl or Python. So I toss a coin and choose Perl. After I've written it, I
find out that Python can do pattern matching ten times faster than Perl. Oops.

While your comments are valid, I'm looking for concrete comparisons. If the
comparisons don't exist, so be it.

Quote:
>I think that you should use the language that you feel
>best fits the work. There is no reason why you should
>only have one size or type of hammer. And a bigger
>hammer does not infer it is better for the task at hand.

>I pick from the languages available when implementing
>software. It was not too long ago when DCL scripts
>and VAX-11 C where the only choices. In the end DCL
>won out as the core vehicle because of a whole
>set of reasons (none personal).

>I have come to love Perl5, but then I love C, Ada, Prolog,
>LISP, BCPL, Algol and a whole range of other languages.

>What I use for work is never decided by personal preference.

>Language wars are a waste of time...

--
PJDM
Peter Mayne
Digital Equipment Corporation (Australia)
Canberra, ACT
----
These are my opinions, and have nothing to do with Digital.
The truth is out there, but not necessarily in here.


Tue, 19 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?
[Mailed and posted]


Peter Mayne

Quote:

> However, what if (for instance) I'm writing a tool that involves lots of
> pattern matching. I could equally well write this tool in (for instance again)
> Perl or Python. So I toss a coin and choose Perl. After I've written it, I
> find out that Python can do pattern matching ten times faster than Perl. Oops.

Care to provide an example? This looks very-very doubtful (provided
you know Perl, no offence - it is possible to _force_ regexps to be
slow in the current perl). I have python, but do not know how to run
it interactively, so _please_ provide a short Python script. People on
Python group may like to see a runnable Perl script too, not just a
scheleton (which is OK with me).

Ilya



Tue, 19 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?
 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,


:
:I agree with your comments below, especially the "language wars are a waste of
:time" one.
:
:However, what if (for instance) I'm writing a tool that involves lots of
:pattern matching. I could equally well write this tool in (for instance again)
:Perl or Python. So I toss a coin and choose Perl. After I've written it, I
:find out that Python can do pattern matching ten times faster than Perl. Oops.

Then you're doing something horribly, horribly wrong in perl; in a word,
pessimal.  I suspect that perl is in fact faster than python at pattern
matching, and I'm happy to prove it.  

Your experiences at poor speed might perhaps stem from the annoying
condition that compiled regexps aren't currently 1st-class citizens, and
so the programmer must work around this.  Fortunately libraries exist to
hide these unniceties.

Here's a simple problem: given a list of patterns, like [vt100 mode
berkeley concept columns switch sysline], write a program that prints out
all lines from stdin that match any of those patterns.

    % time python re1.py < /etc/termcap > /dev/null
    35.95u 0.17s 0:39.52

    % time python re2.py < /etc/termcap > /dev/null
    17.27u 0.09s 0:20.58

    % time perl re0.pl < /etc/termcap > /dev/null
    13.42u 0.13s 0:15.01

    % time perl re1.pl < /etc/termcap > /dev/null
    1.020u 0.110s 0:01.18

    % time perl re2.pl < /etc/termcap > /dev/null
    0.740u 0.120s 0:00.95

This may in fact prove nothing other than that I know how to write very
fast perl code and how to write very slow python code.  Perhaps you have
the opposite experience. :-)

Here's the python code used for re1.py.  (It's probably brain-dead
in some fundamental fashion that I'll hear about for the next month.)

    import sys
    import regexp
    list = []
    for pat in [ 'vt100', 'mode', 'berkeley',
                    'concept', 'columns', 'switch',
                    'sysline' ]:
        list.append(regexp.compile(pat))
    while 1:
            line = sys.stdin.readline()
            if not line: break
            for pat in list:
                if pat.match(line):
                    sys.stdout.write(line)
                    break

Here's the python code used for re2.py:

    import sys
    import regex
    list = []
    for pat in [    'vt100', 'mode', 'berkeley',
                    'concept', 'columns', 'switch',
                    'sysline' ]:
        list.append(regex.compile(pat))
    while 1:
            line = sys.stdin.readline()
            if not line: break
            for pat in list:
                if pat.search(line,0) >= 0:
                    sys.stdout.write(line)
                    break

Here's the straightforward but slow code from re0.pl:

    while (<STDIN>) {
        for $pat (qw[vt100 mode berkeley concept columns switch sysline]) {
            print if /$pat/;
        }
    }

And here's the perl code used for re1.pl (I use my "match_any.pl" library
that returns a closure to deal with the lack of 1st class regexps):

    require 'match_any.pl';
    $match = match_any(qw[vt100 mode berkeley concept columns switch sysline]);
    &$match && print while <STDIN>;

And here's the hand-tuned code for re2.

    $code = " while (<STDIN>) { study; ";
    for $pat (qw[vt100 mode berkeley concept columns switch sysline]) {
        $code .= "print,next if /$pat/;"
    }
    $code .= "}1\n";

I quite doubt whether you'll get this fast in python.

Of course, speed isn't always the issue.  Perl definitely suffers from not
being able to do this in a straightforward re0.  It's really too bad we
don't have this yet:

    use RegExp;
    for $pat (qw[vt100 mode berkeley concept columns switch sysline]) {

    }
    while ($line = <STDIN>) {

            print if $pat->match($line);
        }
    }

But I imagine it's merely a matter of time.  Still, I'd be surprised
if it blazed as fast as re2.pl does.  That one's rather hard to beat.

I'd be even happier if I could pull out perl's awesome matching,
substitute, and translation functions into a C API as Guido has
frequently requested.  I've come up with two apps today where this
would be a great boon, and they weren't even in python. :-)

--tom
--

Republicans understand the importance of {*filter*} between a mother and
child.  -- Vice President Dan Quayle



Wed, 20 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?


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

detto...

Quote:
> And here's the hand-tuned code for re2.

>     $code = " while (<STDIN>) { study; ";
>     for $pat (qw[vt100 mode berkeley concept columns switch sysline]) {
>    $code .= "print,next if /$pat/;"
>     }
>     $code .= "}1\n";

> I quite doubt whether you'll get this fast in python.

but I can go even faster in perl:

--- re3.pl ---
$re = "";
for $pat (qw[vt100 mode berkeley concept columns switch sysline]) {
    $re .= "$pat\|";

Quote:
}

chop($pat);

while(<>){
    print if /$pat/o;

Quote:
}

--------------
yields:

$ time ./re3.pl <./tc >/dev/null
2.17user 0.18system 0:02.37elapsed 99%CPU

vs.

$ time ./re2.pl <./tc >/dev/null
3.44user 0.25system 0:03.70elapsed 99%CPU

ciao,

cm.



Wed, 20 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?
 [courtesy cc of this posting sent to cited author via email]

First of all, your refenences line is broken.  That may mean you're using
a broken or poorly configured mail2news gateway and/or using a broken or
poorly configured mailer feeding into it.

In comp.lang.python,

:The biggest problem I have with perl maintainability (and I have 3
:largish perl scripts I wrote and rely on daily) is its syntax is not
:graceful. One statement can hold a barrel full of monkeys.

Well, Unix people do tend to get used to

    while ( ($some_var = func(42) ) > 20 ) {
        # do something with $some_var, presumably
    }

But it's not like perl invented that notion.

:As far as libraries go, I didn't even know about cpan (still don't
:know where its at), so yes that could be problem.

If you don't know CPAN, your life is impoverished. :-)  Checking
out {ftp,http}://perl.com/perl/ would be a good starting point.

--tom

--

"'My country right or wrong' is like saying, 'My mother drunk or sober.'"
 - G. K. Chesterton



Wed, 20 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?
Tom,

Try these instead of your python examples:

import sys
import regex

def main(pats):
        plist = map(regex.compile, pats)
        psl = map(lambda p:p.search, plist)
        for line in sys.stdin.readlines():
                for s in psl:
                        if s(line) >= 0:
                                sys.stdout.write(line)
                                break

if __name__ == '__main__':
        main(['vt100', 'mode', 'berkeley', 'concept', 'columns',
              'switch', 'sysline' ])

real    0m1.85s
user    0m1.56s
sys     0m0.20s

Or better try:

import sys
import regex

def main(patin):
        pat = regex.compile(patin)
        s = pat.search
        for line in sys.stdin.readlines():
                if s(line) >= 0:
                        sys.stdout.write(line)

if __name__ == '__main__':
        main('vt100\|mode\|berkeley\|concept\|columns\|switch\|sysline')

real    0m0.80s
user    0m0.55s
sys     0m0.15s

All times on a Sun SPARCstation 5, Solaris 2.4 and python 1.3

John
--

Veritas Software
1600 Plymouth St.
Mountain View, CA 94043



Fri, 22 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

   :However, what if (for instance) I'm writing a tool that involves lots of
   :pattern matching. I could equally well write this tool in (for instance again)
   :Perl or Python. So I toss a coin and choose Perl. After I've written it, I
   :find out that Python can do pattern matching ten times faster than Perl. Oops.

   Then you're doing something horribly, horribly wrong in perl; in a word,
   pessimal.  I suspect that perl is in fact faster than python at pattern
   matching, and I'm happy to prove it.  
   ...

It seems to me that the argument you replied to was a purely
hypothetical one, to illustrate the usefulness of tabulated
benchmarks. Noone suggests that Perl takes ten times more time
for a given problem than Python. In fact, I would be very much
surprised if the difference between Perl and Python would ever
reach a factor of ten in any direction for any problem.

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

Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. Centre-Ville     | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)
-------------------------------------------------------------------------------



Fri, 22 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

Quote:
(Hinsen Konrad) writes:
> It seems to me that the argument you replied to was a purely
> hypothetical one, to illustrate the usefulness of tabulated
> benchmarks. Noone suggests that Perl takes ten times more time
> for a given problem than Python. In fact, I would be very much
> surprised if the difference between Perl and Python would ever
> reach a factor of ten in any direction for any problem.

Nevertheless it is VERY educational for anyone who is interested
in what goes fast and why!  Anyone else got any good examples of
common programming tasks and how best to speed them up?

--

High Performance Sports                 Tel 0181-211-7000

My opinions in this forum ARE those of High Performance Sports,
because I'm the only one here who can switch on a PC...



Sat, 23 May 1998 03:00:00 GMT  
 Python vs Perl: benchmarking for speed?

Quote:


>    Then you're doing something horribly, horribly wrong in perl; in a word,
>    pessimal.  I suspect that perl is in fact faster than python at pattern
>    matching, and I'm happy to prove it.  
>    ...

> It seems to me that the argument you replied to was a purely
> hypothetical one, to illustrate the usefulness of tabulated
> benchmarks. Noone suggests that Perl takes ten times more time
> for a given problem than Python. In fact, I would be very much
> surprised if the difference between Perl and Python would ever
> reach a factor of ten in any direction for any problem.

I would not be all that suprised if heavy regexp stuff was an order
of magnitude faster in perl. Python's regexp stuff is nowhere near
as good as perl's, in either speed or functionality. In fact, the
regexp stuff is about the only time I use perl nowadays.

(Although I _am_ actually doing something about this, rather than
just griping :-) but that still requires a bit more work before I have
anything in a state fit to release.)

Anthony



Sat, 23 May 1998 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Perl speed vs. Python speed

2. Speed of Python vs. Perl

3. Speed problems with Python vs. Perl

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

5. Speed of Python vs. Perl

6. python vs. perl speed comparisons

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

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

9. Forth vs Python vs Perl

10. perl vs python vs icon

11. PYTHON VS. PERL VS. TCL

12. jredford's flames and criticism (was: PYTHON VS. PERL VS. TCL )

 

 
Powered by phpBB® Forum Software