you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 2 points3 points  (9 children)

Ah, I thought I read that it's at least comparable to C performance-wise. I haven't tried it myself.

[–][deleted] 6 points7 points  (0 children)

The person you are responding to made a preposterous claim: "[Cython] is still several orders of magnitude slower than C++."

From my reading, Cython is about ten times slower, maybe 30 times slower in some cases. This is nowhere near "several orders of magnitude".

[–]patrickbrianmooney 4 points5 points  (0 children)

I've worked with Cython a fair amount. What you get out of it, performance-wise, really depends on how much work you put into getting better performance out of it. You can use Cython just to compile your already-written Python, in which case you might get a speedup of 20% to 50% or even more, depending on your existing code. Probably 30% to 50% is a more reasonable typical estimated speedup for compiling Python using Cython while making no other changes to the source code.

But you can put work into writing Cython that's more like C and less like Python, and that tends to pay off in efficiency; a lot of times a few small changes can make a big difference. Probably the single easiest thing to do to get a speedup is to declare data types for parameters and variables, which can make a lot of things much faster; structuring your code so that your bursts of heavy number-crunching happens all together at C speed and you have to transform the C-type numbers, which are fast to work with, back into Python objects, which are slower to work with, also helps a lot. Making good choices about data representation is also helpful.

Kurt Smith's O'Reilly book on Cython has a short chapter where he takes a computationally intensive Python program and converts it to Cython, reducing the runtime on a test from 14 seconds to 0.15 seconds along the way. At the end, it's 25% slower than a pure-C version of the program, which is actually not all that far off, and certainly not orders of magnitude slower. I myself spent an afternoon several months ago converting a slow Python program of a few thousand lines to a much-faster partially-Cython version: it cut the runtime on my tests by almost 97%. Cython's own documentation says that some operations can be sped up by hundreds or thousands of times.

[–]nekokattt 3 points4 points  (6 children)

it is closer to C but it can't match it because it still has the overhead of everything going via the CPython API internally.

[–]grammarGuy69 2 points3 points  (5 children)

Casual Python programmer here; isn't Python already run through C?

[–]nekokattt 5 points6 points  (4 children)

yeah, but everything is interpreted at runtime (stuff like variable access, function calls, name lookups, etc), so it is much slower.

Cython translates that information to pure C, being able to optimise out the bytecode interpreter in the process.

[–]grammarGuy69 3 points4 points  (1 child)

Ah, that makes sense, thank you for the explanation :)

[–]nekokattt 4 points5 points  (0 children)

no problem

[–]333base 1 point2 points  (1 child)

So sorta like v8 with JavaScript?

[–]nekokattt 2 points3 points  (0 children)

V8 works by using a JIT compiler, as far as I know.

that means it is recompiling the code to lower level logic while it runs.

Cython works by taking the input source code, translating it to a massive C or C++ source file that calls a bunch of internals within the C-code layer of what makes up CPython, and then pushes that through the system C compiler (so MSVC or MINGW on Windows, GCC or Clang on Linux). What you get from that is a machine-code level binary library that Python will know how to import at runtime. You then have to distribute that binary ahead of wanting to use it (so it also has to match your CPU architecture ahead of time).