5 ms·
A million requests per second in Python, if you write all the code that does work in C.
by ecopoesis 10y ago
A million requests per second in Python, if you write all the code that does work in C.
- andy_ppp 10y agoAnd as little actual python code in the routes as possible...
- bpicolo 10y agoAnd then you get all the fun of debugging clang internals when things go wrong.
- botverse 10y agoYou use it in Python. OP writes the C code for you.
- stefantalpalaru 10y agoYes, but all the performance related praises should go to C.
- pg314 10y agoAs soon as you do anything else than return 'hello, world' in Python, your performance will drop dramatically. Just parsing a tiny JSON string a million times in python takes 7s (i5-2467M). $ time python -c "import json; for i in range(0, 1000000): json.loads('{\"data\": \"hello, world\"}')" real 0m7.055s user 0m6.951s sys 0m0.063s It is no use optimising one part of a system if other parts take 99% of the time. As it stands, the title of the post would be more accurately stated as 'million requests per second with C'.
- SEJeff 10y agoFWIW, you can use ujson, which is much faster than the builtin stdlib json parser as well. I've seen some larger webapps in python do just this.
- Twirrim 10y agojson: $ time python -c "import json; for i in range(0, 1000000): json.loads('{\"data\": \"hello, world\"}')" real 0m3.142s user 0m3.098s sys 0m0.034s ujson $ time python -c "import ujson as json; for i in range(0, 1000000): json.loads('{\"data\": \"hello, world\"}')" real 0m0.444s user 0m0.399s sys 0m0.028s Like with many things, it's a question of being smart about your choice of libraries. Also, just because I was curious: $ time pypy -c "import json; for i in range(0, 1000000): json.loads('{\"data\": \"hello, world\"}')" real 0m0.422s user 0m0.386s sys 0m0.032s
- pg314 10y agoIf you use a C library and have hardly any Python code running it obviously goes faster. That misses the point I was making. At some point you'll have to write some Python code, and that will lower the number of requests dramatically. If you're talking millions of requests per second, you will have maybe 10s of thousands of CPU cycles per request. That doesn't buy you much in Python. Case in point: parsing a trivial json string in Python eats up your budget. Of course, most websites aren't dealing with anywhere near that volume of requests, and then Python is fine. Pypy is interesting, but last time I looked it didn't support C extensions and hence can't be used with the poster's library.
- crdoconnor 10y agotime python -c "import ujson; for i in xrange(0, 1000000): json.loads('{\"data\": \"hello, world\"}')" 0.291s Well, that was an easy way to reduce 7 seconds to 0.3.
- jwilk 10y agoYou might want to use the timeit module instead: $ python -m timeit --setup 'import json' "json.loads('{\"data\": \"hello, world\"}')" 100000 loops, best of 3: 9.34 usec per loop
- deleted 10y ago[deleted]
- throwawaydbfif 10y agoFunny that people bother to do this when there's plenty of other languages that will handle the load without issues. Java Golang Rust Scala Maybe C# C The semi famous techempower benchmarks shows several popular frameworks that can handle over 1mill/sec
- holtalanm 10y agopretty sure Phoenix with Elixir can handle that load too.
- tormeh 10y agoElixir/Erlang is really slow. The strengths of Erlang/Elixir is easy multithreading, easy distributed computing (just as easy as local multithreading), easy fault tolerance and easy low latency. If you have to do something single threaded and performance critical then you do the same thing in Erlang/Elixir as you do in Python: FFI to native code.
- holtalanm 10y agoI wasn't implying Elixir/Erlang was fast. My statement was that it could handle the load. Taken into account the fact that the language is built around concurrency, it would handle the load within multiple threads without a hitch.
- crdoconnor 10y agoBy using C for the hot paths (e.g. HTTP parsing) and python for business logic you can get code that is both faster and easier to maintain than, say, Java.
- weberc2 10y agoHTTP is not the bottleneck of anyone's application. Any interesting business logic will almost certainly cause Python to perform worse than Java, Go, etc. I'm also skeptical of the maintainability claim, because I write Python professionally.
- deleted 10y ago[deleted]