[SciPy-User] Should one NOT start with Cython first?

Sturla Molden sturla.molden at gmail.com
Tue Mar 10 11:47:09 EDT 2015


"Premature optimization is the root of all evil in computer programming." 

Here are my two cents:

- Most code in scientific programming is not CPU bound. You don't get a
faster harddrive or faster network connection from using Cython. This
accounts for the majority (often 80 to 90 %) of the code we write.

- Even if the code is CPU bound the bottleneck might be in a library like
BLAS or LAPACK, for which using Cython might win you nothing. 

- Even in the cases where Cython helps, it might not be worth the effort:
Python code which is fast enough is still fast enough, even if Cython code
is faster. 

- In scientific computing we usually care more about correctness than
speed. Speed is never important befre you have code which is actually
correct.

- Optimizing NumPy code with Numba is often easier than using Cython.

- When programming Cython I often find I want to use C or Fortran instead.

- Bottlenecks are often obscured. Use a profiler to locate them. 

- The best way to deal with a bottleneck is very often to use a better
algorithm ot datastructure, not move from Python to C or Cython.

So should you use Cython from the start? If you are using Cython to
optimize for speed, the answer in my opinion is "no". Make a working Python
prototype and then use a profiler to find the bottlenecks. If you are using
Cython to wrap native code (C, C++, Fortran) then just go ahead and use it
from the beginning.


Regards,
Sturla



Brian Merchant <bhmerchant at gmail.com> wrote:
> Recently, this question was asked on stackoverflow:Cython: when using typed
> memoryviews, are Cython users supposed to implement their own library of
> “vector” functions?
> <<a
> href="http://stackoverflow.com/questions/28948175/cython-when-using-typed-memoryviews-are-cython-users-supposed-to-implement-the/28948871#28948871">http://stackoverflow.com/questions/28948175/cython-when-using-typed-memoryviews-are-cython-users-supposed-to-implement-the/28948871#28948871</a>>
> 
> The current answer suggests that one should not write as much of the
> program as possible using Cython. Rather, one should start with Python, and
> then only use Cython for bottlenecks.
> 
> Is this the right mentality to have when using Cython for scientific
> projects? Why not start Cython first from the get-go?
> 
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> <a
> href="http://mail.scipy.org/mailman/listinfo/scipy-user">http://mail.scipy.org/mailman/listinfo/scipy-user</a>




More information about the SciPy-User mailing list