Yuxiang Wang <yw...@virginia.edu> wrote:

> 4) I wanted to say that it seems to me, as the project gradually
> scales up, Cython is easier to deal with, especially when I am using a
> lot of numpy arrays. If it is even higher dimensional data, it would
> be verbose while it is really succinct to use Cython.

The easiest way to speed up NumPy code is to use Numba which is an LLVM
based JIT compiler. Numba will often give you performance comparable to C
for free. All you have to do is to add the @numba.jit decorator to your
Python function and possibly include some type hints. If all you want is to
speed up NumPy code, just use Numba and it will take care of it in at least
9 of 10 cases.

Numexpr is also a JIT compiler which can speed up Numpy code, but it does
not give as dramatic results as Numba.

Cython is easier to work with than ctypes, particularly when the problem
scales up. If you use typed memoryviews in Cython you can also avoid having
to work with pointer arithmetics. Cython is mainly a competitior to using
the Python C API manually for C extension modules to Python. Cython also
allows you to wrap external C and C++ code, and e.g. use Python and C++
objects together. The drawback is that you need to learn the Cython
language as well as Python and C and C++ and know how they differ. Cython
also have many of the same hidden dangers as C++, due to the possibility of
exceptions being raised between C statements. But because Cython is the
most flexible tool for writing C extensions to Python you will in the long
run do yourself a favor by learning to use it.

ctypes is good when you have a DLL, possibly form a foreign source, and you
just want to use it without any build step. CFFI is easier to work with
than ctypes and has the same usecase. It can parse C headers and does not
require you to define the C API with Python statements like ctypes do.
Generally I would say it is alway better to use CFFI than ctypes. ctypes is
also currently an orphan, albeit in the Python standard library, while CFFI
is actively maintained. 

Numba will also JIT compile ctypes and CFFI calls to remove the extra
overhead. This is good to know if you need to call a C function in a tight
loop. In that case Numba can JIT compile away the Python as well as the
ctypes/CFFI overhead.

Fortran 90/95 is also underrated. It is easier to work with than C, and
gives similar results performance wise. You can call Fortran with f2py,
ctypes, CFFI, or Cython (use fwrap). Generally I would say that it is
better for a student to learn C than Fortran if you have to choose, because
C is also useful for other things than numerical computing. But if you want
fast and robust numerical code, it is easier to get good results with
Fortran than C or Cython.

Sturla

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to