<story><title>Why is Python slow</title><url>http://blog.kevmod.com/2016/07/why-is-python-slow/</url></story><parent_chain><item><author>alt_</author><text>Database died. Google cache: &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;webcache.googleusercontent.com&amp;#x2F;search?q=cache:5V0TMa0cMecJ:blog.kevmod.com&amp;#x2F;2016&amp;#x2F;07&amp;#x2F;why-is-python-slow&amp;#x2F;+&amp;amp;cd=1&amp;amp;hl=en&amp;amp;ct=clnk&amp;amp;gl=uk&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;webcache.googleusercontent.com&amp;#x2F;search?q=cache:5V0TMa0...&lt;/a&gt;&lt;p&gt;The gist of it is:&lt;p&gt;* Python spends almost all of its time in the C runtime&lt;p&gt;This means that it doesn&amp;#x27;t really matter how quickly you execute the &amp;quot;Python&amp;quot; part of Python.  Another way of saying this is that Python opcodes are very complex, and the cost of executing them dwarfs the cost of dispatching them.  Another analogy I give is that executing Python is more similar to rendering HTML than it is to executing JS -- it&amp;#x27;s more of a description of what the runtime should do rather than an explicit step-by-step account of how to do it.&lt;p&gt;Pyston&amp;#x27;s performance improvements come from speeding up the C code, not the Python code.  When people say &amp;quot;why doesn&amp;#x27;t Pyston use [insert favorite JIT technique here]&amp;quot;, my question is whether that technique would help speed up C code.  I think this is the most fundamental misconception about Python performance: we spend our energy trying to JIT C code, not Python code.  This is also why I am not very interested in running Python on pre-existing VMs, since that will only exacerbate the problem in order to fix something that isn&amp;#x27;t really broken.</text></item></parent_chain><comment><author>smegel</author><text>&amp;gt; This means that it doesn&amp;#x27;t really matter how quickly you execute the &amp;quot;Python&amp;quot; part of Python. Another way of saying this is that Python opcodes are very complex, and the cost of executing them dwarfs the cost of dispatching them.&lt;p&gt;That doesn&amp;#x27;t really explain why Python is slow. Your just explaining how Python works. Why should C code be slow? Usually it is fast. Just saying the opcodes are complex doesn&amp;#x27;t really help, because if a complex opcode takes a long time, it is usually because it is doing a great deal.&lt;p&gt;Java used to have the opposite problem. It was doing too much at the &amp;quot;Java bytecode&amp;quot; level, such as string manipulation - so they added more &amp;quot;complex&amp;quot; opcodes written in C&amp;#x2F;C++ to speed things up, significantly.&lt;p&gt;What you really need to explain is why Python is &lt;i&gt;inefficient&lt;/i&gt;. Bloated data structures and pointer hopping for simple things like adding two numbers may be a big reason. I know Perl had many efficiencies built in, and was considered quite fast at some point (90s?).</text></comment>
<story><title>Why is Python slow</title><url>http://blog.kevmod.com/2016/07/why-is-python-slow/</url></story><parent_chain><item><author>alt_</author><text>Database died. Google cache: &lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;webcache.googleusercontent.com&amp;#x2F;search?q=cache:5V0TMa0cMecJ:blog.kevmod.com&amp;#x2F;2016&amp;#x2F;07&amp;#x2F;why-is-python-slow&amp;#x2F;+&amp;amp;cd=1&amp;amp;hl=en&amp;amp;ct=clnk&amp;amp;gl=uk&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;webcache.googleusercontent.com&amp;#x2F;search?q=cache:5V0TMa0...&lt;/a&gt;&lt;p&gt;The gist of it is:&lt;p&gt;* Python spends almost all of its time in the C runtime&lt;p&gt;This means that it doesn&amp;#x27;t really matter how quickly you execute the &amp;quot;Python&amp;quot; part of Python.  Another way of saying this is that Python opcodes are very complex, and the cost of executing them dwarfs the cost of dispatching them.  Another analogy I give is that executing Python is more similar to rendering HTML than it is to executing JS -- it&amp;#x27;s more of a description of what the runtime should do rather than an explicit step-by-step account of how to do it.&lt;p&gt;Pyston&amp;#x27;s performance improvements come from speeding up the C code, not the Python code.  When people say &amp;quot;why doesn&amp;#x27;t Pyston use [insert favorite JIT technique here]&amp;quot;, my question is whether that technique would help speed up C code.  I think this is the most fundamental misconception about Python performance: we spend our energy trying to JIT C code, not Python code.  This is also why I am not very interested in running Python on pre-existing VMs, since that will only exacerbate the problem in order to fix something that isn&amp;#x27;t really broken.</text></item></parent_chain><comment><author>Blaisorblade0</author><text>Are you the OP author, or working on Pyston?
I have basically two questions&amp;#x2F;curiosities — I&amp;#x27;m not asking adversarially:
1) For which code is the C runtime most expensive? Typical Python code tries to leave heavy-lifting in libraries, but what if you write your inner loop in Python? Enabling that is (arguably) one goal of JIT compilation, so that you don&amp;#x27;t need to write code in C.
2) What about using Python ports of performance-sensitive libraries?&lt;p&gt;In more detail:
I arrived at &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;lwn.net&amp;#x2F;Articles&amp;#x2F;691243&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;lwn.net&amp;#x2F;Articles&amp;#x2F;691243&amp;#x2F;&lt;/a&gt;, but I&amp;#x27;m not sure I&amp;#x27;m convinced. Or rather: with a JIT compiler you probably want to rewrite (parts of) C runtime code into Python so you can JIT it with the rest (PyPy has already replaced C code in their implementation, so maybe there&amp;#x27;s work to reuse). For instance, an optimizing compiler should ideally remove abstractions from here:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  import itertools
  sum(itertools.repeat(1.0, 100000000))
&lt;/code&gt;&lt;/pre&gt;
Optimizing that code is not so easy, especially if that involves inlining C code (I wouldn&amp;#x27;t try, if possible), but an easier step is to optimize the same code written as a plain while loop. Does Pyston achieve that? I guess the question applies to the LLVM-based tier, not otherwise.&lt;p&gt;Yes, Python semantics allow for lots of introspection, and that&amp;#x27;s expensive — but so did Smalltalk to a large extent. Yet people managed, for instance, to not allocate stack frames on the heap unless needed (I&amp;#x27;m pointing vaguely in the direction of JIT compilers for Smalltalk and Self, though by now I forgot those details).</text></comment>