Call python code from c via cython

If you rename the quacker.pyx to quacker.py, everything is actually correct. The only problem is that your program won’t search for python modules in the current directory, resulting in the output: Exception NameError: “name ‘quack’ is not defined” in ‘caller.call_quack’ ignored If you add the current directory to the PYTHONPATH environment variable however, the output … Read more

reload module with pyximport?

I was able to get a solution working for Python 2.x a lot easier than Python 3.x. For whatever reason, Cython seems to be caching the shareable object (.so) file it imports your module from, and even after rebuilding and deleting the old file while running, it still imports from the old shareable object file. … Read more

numpy faster than numba and cython , how to improve numba code

As we will see the behavior is dependent on which numpy-distribution is used. This answer will focus on Anacoda-distribution with Intel’s VML (vector math library), millage can vary given another hardware and numpy-version. It will also be shown, how VML can be utilized via Cython or numexpr, in case one doesn’t use Anacoda-distribution, which plugs-in … Read more

Fast string array – Cython

Try following code. to_cstring_array function in the following code is what you want. from libc.stdlib cimport malloc, free from libc.string cimport strcmp from cpython.string cimport PyString_AsString cdef char ** to_cstring_array(list_str): cdef char **ret = <char **>malloc(len(list_str) * sizeof(char *)) for i in xrange(len(list_str)): ret[i] = PyString_AsString(list_str[i]) return ret def foo(list_str1, list_str2): cdef unsigned int i, … Read more

Passing C++ vector to Numpy through Cython without copying and taking care of memory management automatically

I think @FlorianWeimer’s answer provides a decent solution (allocate a vector and pass that into your C++ function) but it should be possible to return a vector from doit and avoid copies by using the move constructor. from libcpp.vector cimport vector cdef extern from “<utility>” namespace “std” nogil: T move[T](T) # don’t worry that this … Read more