37 ms·
There are other python implementations like pypy which includes a JIT (Just In Time compiler). There are other jit which can run with official python (cpython)
by ar9av 4y ago
There are other python implementations like pypy which includes a JIT (Just In Time compiler). There are other jit which can run with official python (cpython) like numba (not all code can be optimized, but if you only need optimize your hot code path).
You can use a superset language of python called cython that generate C code. It can be used to generate C bindings or fast python (for cpython) modules implemented in a python like code.
You can use a really fast language like C, Rust, C++,... create python wrappers with cython, swig, Boost.python, cffi,... and use python like glue code.
Python is not a fast languages as others, but there are tricks to make fast programs.
- a-dub 4y agonumba seems to hit the sweet spot for most numerics for me. it can get a little annoying with type inference issues but overall it seems the most concise and least hassle for moving loops into optimized machine code.
- DeathArrow 4y agoCan you use Django with those optimisations or are they good mainly for scientific computing?
- doix 4y agoAt my old gig we ran Django and FastAPI with pypy. I don't remember there being too many issues. One thing is that pypy versions lag behind official python, so if you're using bleeding edge stuff, it won't be supported in pypy yet. That was a couple of years ago at this point, and I've not been in the python ecosystem since then, but I can only imagine things are getting better in that regard rather than worse.
- rpep 4y agoYou can certainly use it, but whether you see any benefit is going to strongly depend on your workload. If you're doing significant calculations in the API then it might be considerably more performant, but if your API is primarily retrieving things from the database and transforming it to JSON then you're going to be limited mostly by the database latency and so I wouldn't expect major improvements.
- doix 4y agoIf you are fetching lots (not even 'big data', but a few thousand rows) of data using the Django ORM, you will see a performance difference when using pypy, or at least I did a few years ago. The database can happily return a few thousand rows very quickly, especially if you take care to optimize your queries and have good indexes. Converting a few thousand rows to python/django objects takes _time_. I can't quantify anything, because it's been too long, but I remember it being fairly significant. When I profiled it, the majority of the time was spent calling __setattr__ a few million times. Like you said, it depends on your use case. If your queries are slow, then optimize your database queries. But if your queries are fast and your responses are still slow, then investigating pypy is definitely worth it. You can also play around with .values_list or something in Django, so that you get 'raw values' instead of objects (but there's still a cost to building them up).
- coldtea 4y agoAnd yet the same supposedly "io bound" workloads (like "parse request, fetch something from DB, return it as JSON") still have widely difference performance characteristics in some languages vs others, with 10x to 100x requests handled per second...
- acdha 4y ago2x, yes. If you’re seeing 100x you’re comparing different things like creating and serializing complex objects versus simple types or using a JSON parser which loads an entire document into objects versus one which only retrieved specific values.
- coldtea 4y agoWell, perhaps not 100x in rps, but 10x sure: Overall top performing frameworks (JS, Java, and Rust) at 650K rps. That's 7x over the top Python based framework at: 86K rps. And another very popular Python framework (flask) gets just to 2K rps. That's 325 times worse to the best. And that's the "single DB query" benchmark: https://www.techempower.com/benchmarks/#section=data-r21&test=db https://www.techempower.com/benchmarks/#section=data-r21&tes... https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#single-database-query https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Proj...
- jstx1 4y agoHave a look at Cinder - https://github.com/facebookincubator/cinder https://github.com/facebookincubator/cinder - it's Meta's performance oriented fork of CPython that they use to run Instagram (which is a big Django app).
- robertlagrant 4y agoI always wondered with Cinder why they didn't turbocharge PyPy development instead.
- lozenge 4y agoWith a codebase of any significant size the priority is always to maintain compatibility while improving performance. If you start with an incompatible, highly performant interpreter, the compatibility "distance" is difficult to measure and could create unknown performance cost. For example, PyPy doesn't support C modules due to the differing memory layout.
- tekknolagi 4y agoTwo main reasons: * C extensions * Instagram's forking server model I gave a talk that touched on some of this last year: https://2022.ecoop.org/details/ICOOOLPS-2022-papers/5/Cinder-We-didn-t-start-the-fire https://2022.ecoop.org/details/ICOOOLPS-2022-papers/5/Cinder... I wonder why the recording is not up...
- acdha 4y agoI had little trouble switching a Django app years ago but the results were mixed. Some complicated views and reports saw a hefty win but most of the app was database-limited and optimized to the point that there was no meaningful difference, except that PyPy used more RAM.
- jstarfish 4y agoPypy is great but I didn't find it very useful with Django. Quick, transactional HTTP exchanges (GET, POST, etc.) aren't really its thing-- there's no time for the compiler to get warmed up; the request is complete before pypy has gotten out of bed. But if you have to do really complex view rendering (graphs or something) where it would take cpython ~10s or more to process, then pypy will leave cpython in the dust.
- afdbcreid 4y agoI've never used PyPy, but shouldn't it warm after the Nth request even if you don't have loops?
- adammarples 4y agoOne great move I've discovered recently is to simply type annotate the python thoroughly and use mypyc to build a c package
- wcdolphin 4y agoWould you mind sharing more details of your experience using MypyC? What domain are you working in, and what kind of effort and speedups did you see?
- FreakLegion 4y agoHere's a detailed write-up from one of the Black maintainers: https://ichard26.github.io/blog/2022/05/compiling-black-with-mypyc-part-1/ https://ichard26.github.io/blog/2022/05/compiling-black-with.... That's on the low side of the speedup curve, in my experience.