5

I've recently taken up Python3 and was stumped by how much slower than other comparable dynamic languages (mainly Perl) it is.

While trying to learn Python I did several online coding challenges and Python would often be at least 10x slower than Perl and use at least 2x the memory.

Researching this curiosity I came across people asking why Python is slower than C/C++, which should be pretty obvious, but not any posts comparing it to other similar languages. There is also this informative but outdated benchmark http://raid6.com.au/~onlyjob/posts/arena/ which confirms it being rather slow.

I am explicity asking about the standard Python implementation and NOT anything like pypy or the likes.

EDIT: The reason I was surprised comes from the results page on codeeval.com. Here are two scripts to capitalize the first character of every word in a line.

Python3 (3.4.3) v1

import sys
import re

def uc(m):
    c = m.group(1)
    return c.upper()

f = open(sys.argv[1], "r")
for line in f:
    print(re.sub(r"\b(\D)", uc, line))

Perl (5.18.2)

use strict;
use warnings "all";

open(my $fh, "<", "$ARGV[0]") or die;
while (<$fh>)
{
    s,\b(\D),uc $1,ge;
    print;
}
close $fh;

As I am not very familiar with Python yet I also tried a different version to see if there was any difference.

Python3 v2:

import sys

f = open(sys.argv[1], "r")
for line in f:
    lst = [word[0].upper() + word[1:] for word in line.split()]
    print(" ".join(lst))

The results are quite different as can be seen in this image: https://i.imgur.com/3wPrFk5.png (results for Python in this image are from the v1, v2 had nearly identical stats (+1 ms execution time, ~same memory usage)

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Annie May
  • 69
  • 11
  • 6
    Way too broad, show a specific small reproducible code snippet that is slower in Python than Perl (showing the code in both languages) then maybe you have a question – Chris_Rands May 18 '17 at 11:46
  • 6
    The generalization is not true. For example, in the [benchmarks game](https://benchmarksgame.alioth.debian.org/u64q/perl.html) Python is not uniformly slower than Perl. – Sinan Ünür May 18 '17 at 12:02
  • @SinanÜnür Someone claimed they don't use the standard Python implementation, I don't know if that us true or not. – Annie May May 18 '17 at 13:08
  • You are measuring the regular expression engine more than the language itself. – chepner May 18 '17 at 13:27
  • Hence the Python version with out RE – Annie May May 18 '17 at 13:29
  • `line.rstrip` is neither needed nor in the Perl script to which you are comparing. – Sinan Ünür May 18 '17 at 13:49
  • You are correct, it was a leftover from an older revision. I'll take it out. (Doesn't seem to influence the performance though) – Annie May May 18 '17 at 14:24
  • @Annie May >> I don't know if that us true or not << Check for yourself -- the provided URL tells you which Perl and Python versions were used. – igouy May 18 '17 at 23:22
  • 2
    To make an unreasonable generalization in a comment to a question that is itself a huge unreasonable generalization -- Python's audience is largely composed of people who care more about readability and maintainability, and less about performance; those who care about performance in addition to Python's traditional readability goals have largely migrated towards Go (if they also care about maintainability) or Julia (for math-heavy, performance-sensitive work when maintainability -- particularly inclusive of language stability -- is less important). Perl's audience... *hah*. – Charles Duffy May 19 '17 at 00:06
  • Your benchmark is absolutely meaningless because it's only a couple of milliseconds – you are mostly benchmarking the interpreter startup time, not the actual performance. Try working through a couple hundred MB of data for more informative results. Also, please use `re.compile()` to pre-compile the Python regex once. Perl does this automatically. Finally, regexes are one of Perl's strong points. It's hard to beat Perl for regex-heavy benchmarks. – amon May 20 '17 at 21:01

0 Answers0