Questions tagged [cython]

Cython is a superset of the Python language for quickly generating Python C extensions.

Cython is a superset of the Python language for quickly generating Python C/C++ extensions. Cython is a pidgin language of Python and C/C++. Unlike pure Python, Cython code is not directly interpreted by the Python interpreter, but is instead used to generate C/C++ code. The generated C/C++ code can then be compiled into a C/C++-extension, which then can be imported by Python code.

Cython syntax was originally based on Python 2, with added type declarations à la C/C++, however, its syntax now supports both Python 2 and 3 language features. Additionally, Cython is capable of generating C/C++ extension code compatible with either Python 2 or Python 3. Its syntax now allows the use of advanced C++ constructs such as template and stl container. Finally, thanks to static typing, Cython code generally executes much faster than Python code.

Cython is freely available under the open source Apache License.

The latest release of Cython is 3.0 alpha 5 (released 2020-05-19). Cython is available from the PyPI package index repository.

5220 questions
3
votes
1 answer

Cython: unable to call non static methods of shared library

My problem is pretty simple, I have a shared library compiled with CMake and I want to write a python wrapper for one cpp class it contains and a standalone function. Let's say the class is this with the standalone function // cppClass.cpp void…
Romzie
  • 457
  • 4
  • 11
3
votes
1 answer

Taking a Set of a List created using C in Cython is much slower than pure Python - Why?

In this example, I show two different methods for creating a list of strings using Cython. One uses an array of char pointers (and the strcpy C function) and the other by simply appending elements to a list. I then pass each of these lists into the…
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136
3
votes
1 answer

Cython with Python3.5: convert list of string to char**

I am trying to pass a list of string to a C++ function that takes a char** as argument using Cython. I tried other solutions I can't remember but I mainly tried the two following options: Using a convertion function extracted from here, which is…
Romzie
  • 457
  • 4
  • 11
3
votes
0 answers

Cython: disable automatic type conversion

I have one cpp file like this: namespace MyNamespace { struct MyStruct { …
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
3
votes
1 answer

Cython return malloced pointer from function

I am fairly new to Cython, so this is probably fairly trivial, but I haven't been able to find the answer anywhere. I've defined a struct type and I want to write a function that will initialize all the fields properly and return a pointer to the…
Pavlin
  • 5,390
  • 6
  • 38
  • 51
3
votes
2 answers

Fastest way to find unique rows of 2D NumPy array with Cython

I have a 2D NumPy array that could be of any type, but for this example, we can assume it is integers. I am looking to find the fastest way to find all the unique rows in the array. My initial strategy was to convert each row into a tuple and add…
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136
3
votes
1 answer

Why is the build-in array.fromlist() slower than cython-code?

Often, when gluing Python and C-code together, one needs to convert a Python-list to a continuous memory, e.g. an array.array. It's also not unusual, that this conversion step becomes the bottle-neck, so I find myself doing silly things with Cython…
ead
  • 32,758
  • 6
  • 90
  • 153
3
votes
3 answers

How can I make this image processing function faster? Already tried Cython

I'm currently trying to implement a function in Python that is supposed to find occurrences of a certain color value in an image in order to determine the bounding box of a color region. This seems to work, albeit at a very slow speed. Iterating…
dawg_91
  • 59
  • 5
3
votes
1 answer

Foreign function: who deallocates memory?

I'm calling a Go function from Python. Go function returns a string, specifically, a GoString with the string itself being allocated on Go's side. Question Who's responsible for deallocation of this memory? A very simplified example follows. Go…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
3
votes
1 answer

Inverting a matrix using the LU decomposition

I have written the following Matrix class in cython for the matrix inversion and some other linear algebra operations. I tried to use the LU decomposition, in order to compute the inverse of a matrix. The speed of code is good. I tried to implement…
Dalek
  • 4,168
  • 11
  • 48
  • 100
3
votes
1 answer

Suppress warnings from Cython

I cythonize *.pyx files manually using cython -3 -Wextra mymodule.pyx I use the -Wextra option to generate additional warnings, useful for cleaning up redundant code pieces. However, many warnings of the form warning: mymodule.pyx:123:45: local…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
3
votes
1 answer

Cython: understanding what the html annotation file has to say?

After compiling the following Cython code, I get the html file that looks like this: import numpy as np cimport numpy as np cpdef my_function(np.ndarray[np.double_t, ndim = 1] array_a, np.ndarray[np.double_t, ndim = 1] array_b, …
Behzad Jamali
  • 884
  • 2
  • 10
  • 23
3
votes
1 answer

Cython: How to declare numpy.argwhere()

I tried to Cythonize part of my code as following to hopefully gain some speed: # cython: boundscheck=False import numpy as np cimport numpy as np import time cpdef object my_function(np.ndarray[np.double_t, ndim = 1] array_a, …
Behzad Jamali
  • 884
  • 2
  • 10
  • 23
3
votes
1 answer

How do I cast a slice of memoryview into C string (unsigned char*)?

Below is the code where I'm having this problem: cpdef object encode_file(object fin, str fout): if not PyObject_CheckBuffer(fin): raise TypeError("fin must follow the buffer protocol") cdef Py_buffer in_view cdef int ret_code…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
3
votes
0 answers

Cythonizing specific methods of a class written in Python

I have an A* planning algorithm written purely in Python (using this excellent resource on path planning). The grid class with associated methods for movement and costs is defined as follows: class SquareGrid: def __init__(self, width,…
wheatley
  • 51
  • 4