TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why Python is Slow: Looking Under the Hood (2014)

170 pointsby s16hover 7 years ago

14 comments

wruzaover 7 years ago
&gt;[boxed values...] The dynamic typing means that there are a lot more steps involved with any operation. This is a primary reason that Python is slow compared to C for operations on numerical data.<p>Not exactly. Setting typecodes and vals doesn’t slow things down by many orders of magnitude. The main reason python is relatively slow is that there is no practical way to reason about what parts of program may be optimized out or leveled down to native datatypes and then restructured in a very efficient way. This is what optimizing&#x2F;jit compilers do to achieve much performance; this one is the source of x100, not unboxing on its own. Technically, tracing jit that doesn’t care if you’re writing in static or dynamic, strict or duck typing can be done for any language, but (afaik) python is not very jit-friendly in general.
评论 #15549707 未加载
评论 #15550356 未加载
评论 #15550515 未加载
评论 #15551492 未加载
评论 #15549580 未加载
dnauticsover 7 years ago
Julia is all three, yet it&#x27;s fast (allowing for jit, which in Julia is a one time cost). It&#x27;s worth noting that those properties themselves are not what&#x27;s critical, so much as designing around it and allowing for fast paths through critical code (locking down the dynamic types, first class array datatypes with packed forms)...
评论 #15549053 未加载
评论 #15549349 未加载
评论 #15550465 未加载
hasenjover 7 years ago
Python is optimized for small programs being easy and quick (and pleasant) to write.<p>Every other use case it fails in some way. For being slow. For lacking static typing. For lacking compilation. For having a GIL. etc.
评论 #15549379 未加载
评论 #15549514 未加载
评论 #15549330 未加载
评论 #15549399 未加载
评论 #15549487 未加载
lorenzfxover 7 years ago
I believe this article [0] (previous discussion [1]) from one of pyston&#x27;s authors gives a <i>much</i> better overview of why python is slow.<p>[0] <a href="http:&#x2F;&#x2F;blog.kevmod.com&#x2F;2016&#x2F;07&#x2F;why-is-python-slow&#x2F;" rel="nofollow">http:&#x2F;&#x2F;blog.kevmod.com&#x2F;2016&#x2F;07&#x2F;why-is-python-slow&#x2F;</a> [1] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12025309" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12025309</a>
mikebenfieldover 7 years ago
Although I use and enjoy Python for some purposes, I can&#x27;t help by see all the effort gone into improving Python performance (including Pypy, Cython, rewriting code in C, etc) as fixing a self-inflicted wound. Why are we using a language whose semantics make it very difficult to execute quickly?
评论 #15550044 未加载
评论 #15550201 未加载
评论 #15550079 未加载
chmaynardover 7 years ago
From the article: &quot;Dynamic typing makes Python easier to use than C.&quot; The author gives no justification for this claim. Do any language experts care to comment?
评论 #15549101 未加载
评论 #15549215 未加载
评论 #15549111 未加载
评论 #15549778 未加载
adenadelover 7 years ago
I thought this was pretty neat<p><pre><code> # WARNNG: never do this! id113 = id(113) iptr = IntStruct.from_address(id113) iptr.ob_digit = 4 # now Python&#x27;s 113 contains a 4! 113 == 4 </code></pre> And now since the proper value 113 doesn&#x27;t exist you have to resort to the binary representation on your system to revert back to normal behavior<p><pre><code> ctypes.cast(id113, ctypes.POINTER(ctypes.c_char))[3 * 8] = b&#x27;\x71&#x27;</code></pre>
评论 #15550404 未加载
nayukiover 7 years ago
I agree with this article. In practice, when writing number-crunching code in Python versus Java, I found that Python is usually 10 to 30× slower than Java, sometimes even 100× slower. See: <a href="https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;project-euler-solutions#benchmark-timings" rel="nofollow">https:&#x2F;&#x2F;www.nayuki.io&#x2F;page&#x2F;project-euler-solutions#benchmark...</a>
ryanpcmcquenover 7 years ago
Counter? <a href="https:&#x2F;&#x2F;hackernoon.com&#x2F;yes-python-is-slow-and-i-dont-care-13763980b5a1" rel="nofollow">https:&#x2F;&#x2F;hackernoon.com&#x2F;yes-python-is-slow-and-i-dont-care-13...</a>
tjpnzover 7 years ago
And yet there are entire industries built on the back of it. Everytime you see a a movie there&#x27;ll be some Python code somehow responsible for the pixels you&#x27;re seeing on the big screen.
评论 #15550539 未加载
jokoonover 7 years ago
How compatible are existing python libraries with pypy, and is the official python taking clues from pypy? Is there more work to do to make pypy even faster?
评论 #15549463 未加载
评论 #15554415 未加载
baybal2over 7 years ago
A thing much bigger than GIL for Python is that in python bytecode, objects are used as primitives.
anon1253over 7 years ago
Except it isn&#x27;t really. Yes, Python is incredibly slow for day to day stuff, but the sheer amount of easy to use numerical libraries (numpy, scipy, scikit-learn, tensorflow, keras, opencv, just to name a few) make it one of the fastest out there for numerical computation. I tried doing some numerical heavy stuff on the JVM (with Java and Clojure) and it fights you every step of the way ... and that has static typing and all the things the article mentions. Of course, Python derives that functionality from C and Fortran … but just having that interop at your fingertips is magical in terms of productivity. I still get nightmares from working with the JNI.
评论 #15549226 未加载
评论 #15549239 未加载
评论 #15549225 未加载
lispmover 7 years ago
&gt; Python being a dynamically typed, interpreted language<p>&#x27;CPython&#x27; is the defining implementation of the dynamically typed language &#x27;Python&#x27; using a byte-code VM (and no jit compiler).<p><pre><code> bash-3.2$ time &#x2F;tmp&#x2F;bench.py 5000000050000000 real 0m19.763s user 0m15.015s sys 0m4.309s </code></pre> &#x27;SBCL&#x27; is an implementation of the dynamically typed language &#x27;Common Lisp&#x27; using a native code compiler.<p><pre><code> bash-3.2$ time &#x2F;tmp&#x2F;bench.lisp 5000000050000000 real 0m0.319s user 0m0.294s sys 0m0.017s </code></pre> The unoptimized code is roughly 65 times faster in SBCL compared to CPython - including startup time.
评论 #15549746 未加载
评论 #15549484 未加载