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.

How slow is Python really? Or how fast is your language?

128 pointsby kisamotoabout 11 years ago

17 comments

wtingabout 11 years ago
Python is fast enough, until it isn&#x27;t and then there are no simple alternatives.<p>If your problem is numerical in nature, you can call popular C modules (numpy, etc) or write your own.<p>If your functions and data are pickleable, you can use multiprocessing but run into Amdahl&#x27;s Law.<p>Maybe you try Celery &#x2F; Gearman introducing IO bottlenecks transferring data to workers.<p>Otherwise you might end up with PyPy (poor CPython extension module support) and still restricted by the GIL. Or you&#x27;ll try Cython, a bastard of C and Python.<p>Python has been my primary language the past few years and it&#x27;s great for exploratory coding, prototypes, or smaller projects. However it&#x27;s starting to lose some of the charm. Julia is filling in as a great substitute for scientific coding in a single language stack, and Go &#x2F; Rust &#x2F; Haskell for the other stuff. I&#x27;ve switched back to the static language camp after working in a multi-MLOC Python codebase.
评论 #7673444 未加载
评论 #7673371 未加载
评论 #7673574 未加载
评论 #7673620 未加载
评论 #7673465 未加载
Wilyaabout 11 years ago
Using a Numpy-based program as an example of how Python can be fast is a bit strange.<p>It shows that Python can be fast enough <i>if you leave the heavy computational parts to libraries written in other languages</i>. Which is interesting, but doesn&#x27;t say much about the speed of Python itself.
评论 #7673463 未加载
评论 #7672873 未加载
评论 #7674381 未加载
评论 #7672675 未加载
评论 #7672744 未加载
moeabout 11 years ago
Anecdotical datapoint: For a great many years I used to consider python a very slow language. Then I switched to Ruby and realized how slow a language can <i>really</i> be and yet still be practical for many use-cases.
评论 #7673785 未加载
评论 #7673288 未加载
lambyabout 11 years ago
Obviously, this is a silly benchmark and we should stop giving it any credit.<p>However, even &quot;real world&quot; anecdotes in this area can be a minefield.<p>Take, for example, an existing Python application that&#x27;s slow which requires a rewrite to fix fundamental architectural changes.<p>Because you feel you don&#x27;t need necessarily need the flexibility of Python the second time around (as you&#x27;ve moved out of the experimental or exploratory phase of development), you decide to rewrite it in, say, Go, or D or $whatever.<p>The finished result turns out to be 100X faster—which is great!—but the danger is always there that you internalise or condense that as &quot;lamby rewrote Python system X in Go and it was 100X faster!&quot;
chrisBobabout 11 years ago
I spend a lot of time debating program speed (mostly C vs MATLAB), but the problem is that the programming and compile time usually makes more of a difference than people consider.<p>If my C is 1000x faster and saves me 60 seconds every time I run the program, but takes an extra 2 days to write initially, and the program is seeing lots of edits meaning that on average I have to wait 2 minutes for it to compile then I am MUCH better off with the <i>slower</i> MATLAB until I am running the same thing a few thousand times.<p>Plus there is the fact that I can look at HN while a slightly slower program is running, so I win both ways.
评论 #7673178 未加载
评论 #7673448 未加载
ahogeabout 11 years ago
Numpy is written in C. That&#x27;s why it&#x27;s fast.<p>Better: <a href="http://benchmarksgame.alioth.debian.org/" rel="nofollow">http:&#x2F;&#x2F;benchmarksgame.alioth.debian.org&#x2F;</a>
评论 #7672741 未加载
评论 #7673943 未加载
评论 #7673557 未加载
评论 #7673960 未加载
fmdudabout 11 years ago
<i>Holy false comparison, Batman!</i><p>Why would you use Numpy for arrays that small? Oh, looks like someone actually just wrote it in CPython, no Numpy, and it clocked in at 0.283s. Which is fine. It&#x27;s Python.<p>This thread reminds me of the scene in RoboCop where Peter Weller gets shot to pieces. Peter Weller is Python and the criminals are the other languages.
Igglybooabout 11 years ago
Judging by the top submission being also written in python, I think this just shows how unoptimized OP&#x27;s original code was rather than how slow the language is.<p>Not that python is fast, it isn&#x27;t. And using numpy seems a bit disingenuous anyways &quot;Oh my python program is faster because I use a library that&#x27;s 95% C&quot;
评论 #7676096 未加载
jzwinckabout 11 years ago
The same author previously posted this code as a question on Stack Overflow: <a href="http://stackoverflow.com/questions/23295642/" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;23295642&#x2F;</a> (but we didn&#x27;t speed it up nearly as much as the Code Golf champions).<p>If you enjoyed this Python optimization, you may also enjoy: <a href="http://stackoverflow.com/questions/17529342/" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;17529342&#x2F;</a><p>This sort of thing comes up a lot: people write mathematical code which is gratuitously inefficient, very often simply because they use a lot of loops, repeated computations, and improper data structures. So pretty much the same as any other language, plus the extra subtlety of knowing how and why to use NumPy (as it turned out, this was not a good time for it, though that was not obvious).
julesabout 11 years ago
You can make this far faster by changing the data representation. You can represent S as a bit string so that if the i&#x27;th bit is 0 then S[i] = 1 and if the i&#x27;th bit is 1 then S[i] = -1. Lets call that bit string A. You can represent F as two bit strings B,C. If the i&#x27;th bit in B is 0 then F[i] = 0. If the i&#x27;th bit of B is 1 then if the i&#x27;th bit of C is 0 then F[i] = 1 else F[i] = -1. Now the whole thing can be expressed as parity((A &amp; B) ^ C). The parity of a bit string can be computed efficiently with bit twiddling as well. Now the entire computation is in registers, no arrays required. The random generation is also much simpler, since we only need to generate random bit strings B,C and this is already directly what random generators give us. I wouldn&#x27;t be surprised if this is 1000x faster than his Python.
alexchamberlainabout 11 years ago
It&#x27;s really fast to develop in, and with NumPy&#x2F;Pandas&#x2F;Scipy it runs numerical models fairly fast too. You do have to spend time getting to know `cProfile` and `pstats`; saved over 80% on runtime of something the other day.
pjmlpabout 11 years ago
What should the OP ask instead:<p>&quot;How fast is the code produced by your compiler.&quot;<p>I keep seeing this misconception about languages vs implementations.<p>EDIT: Clarified what my original remark meant.
评论 #7673114 未加载
评论 #7672919 未加载
评论 #7672841 未加载
donniezazenabout 11 years ago
As a new programmer with only Java(Android) under my arms, I find the whole concept of &quot;your language&quot; mind boggling.
评论 #7673466 未加载
taudeabout 11 years ago
Go&#x2F;Scala, etc. programmers, go add your answer to the OPs S.E. answer. Lots of other entertaining coding examples there.
WoodenChairabout 11 years ago
Summary: Question asker wrote a program in Python using numpy (A Python library that calls C code) which could&#x27;ve been more performant if written in pure Python (something to do with array sizes being used) and Python in general is slower than C&#x2F;C++&#x2F;Fortran&#x2F;Rust. Anything else new?
评论 #7673736 未加载
malkiaabout 11 years ago
Python is slow, but handy for automation.
chrismorganabout 11 years ago
Yet another attempt at a comparison scuttled by using randomness.<p>Different things use different types of randomness. Some are fast. Some are slow. If your comparison is not using the same type of randomness, that comparison is comparatively useless.
评论 #7673334 未加载