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.

Optimizing Python – A Case Study

127 pointsby dpmehta02almost 10 years ago

6 comments

vosperalmost 10 years ago
He&#x27;s missing Cython, which is another good option when you&#x27;re looking for speed.<p>My personal favourite optimisation, from needing to shave a few milliseconds off our API response times, was discovering that it&#x27;s measurably slower to use * args and * *kwargs, and switching to explicitly declaring and passing arguments in the relevant parts of the code.<p>We also did a few other neat things:<p>- Rolled our own UUID-like generator in pure Python (I was surprised this helped, but the profiler doesn&#x27;t lie)<p>- Switched to working directly with WebOb Request and Response objects rather than using a framework<p>- Used a background thread with a single slot queue to make sure our response was returned to the user before we emitted the event log message, but always emit the message before moving to the next request<p>- Heavy optimisation of memcache &#x2F; redis reads and writes<p>Edit: Fixed formatting
评论 #9622667 未加载
sitkackalmost 10 years ago
The order of tactics to take is wrong. In terms of energy expended, one should use PyPy first! It is amazingly compatible with CPython and can now be embedded directly in CPython programs, <a href="https:&#x2F;&#x2F;github.com&#x2F;fijal&#x2F;jitpy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fijal&#x2F;jitpy</a> (supports numpy arrays)<p>Dump your virtualenv, create a new one with pypy, reinstall libraries and test your app. Takes less than 20 minutes, even for complex applications.
评论 #9623092 未加载
评论 #9622966 未加载
评论 #9626370 未加载
clickokalmost 10 years ago
Serious question: if you have some code that really has to be fast, is it viable to keep it in Python, or should you ultimately end up rewriting it in a compiled language?<p>For example, I am writing code that implements networks that evolve over time for AI research. Prototyping it in Python makes it easy to test things out, but I expect that I will have to rewrite it in C++ or maybe something more fun, like Haskell[1].<p>1. Mostly for the sheer joy of trolling my colleagues with a learning agent monad.
评论 #9622487 未加载
评论 #9622941 未加载
评论 #9623191 未加载
评论 #9624774 未加载
评论 #9622734 未加载
pepijndevosalmost 10 years ago
&gt; Think for a second: time is only ever going to increase<p>Well, most of the time at least. Think about DST and leap seconds.
评论 #9626400 未加载
mangelettialmost 10 years ago
Isn&#x27;t Jython <i>not</i> a JIT compiler, and isn&#x27;t Jython much slower than cPython?
评论 #9622287 未加载
评论 #9622217 未加载
评论 #9622488 未加载
aburan28almost 10 years ago
Pythran is also missing
评论 #9624715 未加载