Numpy vs Cython speed

前端 未结 5 1531
清酒与你
清酒与你 2020-11-28 22:37

I have an analysis code that does some heavy numerical operations using numpy. Just for curiosity, tried to compile it with cython with little changes and then I rewrote it

5条回答
  •  一生所求
    2020-11-28 22:56

    I would recommend using the -a flag to have cython generate the html file that shows what is being translated into pure c vs calling the python API:

    http://docs.cython.org/src/quickstart/cythonize.html

    Version 2 gives nearly the same result as Version 1, because all of the heavy lifting is being done by the Python API (via numpy) and cython isn't doing anything for you. In fact on my machine, numpy is built against MKL, so when I compile the cython generated c code using gcc, Version 3 is actually a little slower than the other two.

    Cython shines when you are doing an array manipulation that numpy can't do in a 'vectorized' way, or when you are doing something memory intensive that it allows you to avoid creating a large temporary array. I've gotten 115x speed-ups using cython vs numpy for some of my own code:

    https://github.com/synapticarbors/pylangevin-integrator

    Part of that was calling randomkit directory at the level of the c code instead of calling it through numpy.random, but most of that was cython translating the computationally intensive for loops into pure c without calls to python.

提交回复
热议问题