Speed of Python vs. Perl

Daniel Chetlin daniel at chetlin.com
Thu Jan 11 07:00:04 EST 2001


On Thu, 11 Jan 2001 08:54:17 GMT,
 Tim Hammerquist <tim at degree.ath.cx> wrote:
>Michael P. Soulier <msoulier at storm.ca> wrote:
[snip]
>> [msoulier at lupus test]$ time ./bench.pl
>> 
>> real    0m2.006s
>> user    0m1.950s
>> sys     0m0.030s
>> 
>> [msoulier at lupus test]$ time ./bench.py
>> 
>> real    0m0.558s
>> user    0m0.440s
>> sys     0m0.050s
>
>How much of these times is the startup time for the interpreters?
>Python has a profiler, and the Standard Perl Dist. includes the
>Benchmark module for this purpose. This would give you a more accurate
>picture of where the processor spends its time.

I sat on a response to Michael's post for about an hour, and finally
decided not to send it, being sure that it would be misinterpreted as an
attempt to start a pyvspl flamewar. However, since Tim has responded
here, I figure I'll pass on my original post.

Executive summary: the numbers given above by Michael are not a good
indication of real comparative speed between similarly implemented
programs in Python and Perl.

Final disclaimer: what I write below is in no way intended to make a
claim about relative speed, usability, or karma of any language. It is
simply to clear up a minor misconception.

On Sun, 07 Jan 2001 19:52:22 GMT,
 Michael P. Soulier <msoulier at storm.ca> wrote:
[snip]
>[msoulier at lupus test]$ cat bench.pl
>#!/usr/bin/perl
>
>%hash = ();
>for ($i = 0; $i < 100000; ++$i) {
>    $hash{$i} = 1;
>}
>
>    This is a typical perl implementation of a loop. The use of the for
>loop and its integer operations though, don't help the performance. If
>we instead use Python's range function, we'll probably get better
>performance.

Actually, that's not at all a typical Perl loop. The majority of Perl
gurus would infer from reading that code that the programmer is mired in
C-land and hasn't yet seen the usual Perl idioms. If iterating over
numbers is necessary, the idiom would probably be:

  for (0 .. 99999) {

But in most cases, one would iterate directly over elements, rather than
creating a list of numbers. And in this case, one might eschew the loop
altogether:

  @hash{0 .. 99999} = (1) x 99999;

The advantages of this construct are clarity and conciseness at the
expense of a bit of speed -- the for-loop with range operator will
likely be a bit faster -- while remaining substantially faster than the
C-style loop.

Your general point -- that implementation matters and that Python
certainly has areas where it is faster than Perl -- is still quite
valid, and I do not disagree. You will find, however, that using the
range operator or the hash slice speeds up the Perl significantly.

</de-lurk>

-dlc




More information about the Python-list mailing list