title: Why is my perl script an order of magnitude faster than the equivalent python code
body: <p>I've recently taken up Python3 and was stumped by how much slower than other comparable dynamic languages (mainly Perl) it is.</p>
  
  <p>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.</p>
  
  <p>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 <code>http://raid6.com.au/~onlyjob/posts/arena/</code> which confirms it being rather slow.</p>
  
  <p>I am explicity asking about the standard Python implementation and NOT anything like pypy or the likes.</p>
  
  <p>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.</p>
  
  <p>Python3 (3.4.3) v1</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>Perl (5.18.2)</p>
  
  <pre><code>use strict;
  use warnings "all";
  
  open(my $fh, "&lt;", "$ARGV[0]") or die;
  while (&lt;$fh&gt;)
  {
      s,\b(\D),uc $1,ge;
      print;
  }
  close $fh;
  </code></pre>
  
  <p>As I am not very familiar with Python yet I also tried a different version to see if there was any difference.</p>
  
  <p>Python3 v2:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>The results are quite different as can be seen in this image: <a href="https://i.stack.imgur.com/NiiNP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NiiNP.png" alt="https://i.imgur.com/3wPrFk5.png"></a> (results for Python in this image are from the v1, v2 had nearly identical stats (+1 ms execution time, ~same memory usage)</p>