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
16
votes
1 answer

Setup of PyCharm for Cython

I see that PyCharm supports Cython. I could always compile and run in terminal, but I'm wondering if there is a way to do this in PyCharm. In the link it says: "Compilation is done using external tools. The preferred build systems (Makefile,…
eharbitz
  • 489
  • 1
  • 3
  • 11
16
votes
1 answer

How to package a linked DLL and a pyd file into one self contained pyd file?

I am building a python module with Cython that links against a DLL file. In order to succesfully import my module I need to have the DLL in the Windows search path. Otherwise, the typical error message is: ImportError: DLL load failed: The specified…
toine
  • 1,946
  • 18
  • 24
16
votes
2 answers

How to store python objects in Cython C++ containers?

I would like to port an existing c++ library with cython to Python, with the C++ library employing templates. In this case, it is the adevs library. The question is how can I store Python objects in a C++ container with Cython? I know this is…
16
votes
2 answers

Numba and Cython aren't improving the performance compared to CPython significantly, maybe I am using it incorrectly?

BIG EDIT: ================ For the sake of clarity, I am removing the old results and replace it by the more recent results. The question is still the same: Am I using both Cython and Numba correctly, and what improvements to the code can be made?…
user2489252
16
votes
3 answers

Cython and numpy speed

I'm using cython for a correlation calculation in my python program. I have two audio data sets and I need to know the time difference between them. The second set is cut based on onset times and then slid across the first set. There are two…
jushie
  • 171
  • 1
  • 5
16
votes
3 answers

Why is this code slower in Cython than in Python?

I'm starting to learn Cython because of performance issues. This particular code is an attempt to implement some new algorithms in the transportation modeling (for planning) area. I decided to start with a very simple function that I will use a LOT…
PCamargo
  • 584
  • 6
  • 26
15
votes
2 answers

Creating C structs in Cython

I'd like to create my very own list container using Cython. I'm a very new begginer to it, and following the documentation I could get to creating such a structure : cdef struct s_intList: int value void* next ctypedef s_intList…
Oleiade
  • 6,156
  • 4
  • 30
  • 42
15
votes
1 answer

How do I pass a pointer to a c function in Cython?

I'm trying call qsort in Cython with a custom compare function but I don't understand how to pass the function reference. First, I have a struct: cdef struct Pair: int i,j float h The compare function sorts by h: cdef int…
Rich
  • 12,068
  • 9
  • 62
  • 94
15
votes
1 answer

Efficient structure for element wise access to very large sparse matrix (Python/Cython)

I'm looking for an efficient data structure to represent a very large matrix of integers in Python/Cython with focus on element-wise operations. I'm currently building a model that requires a lot of element-wise operations on a large, highly sparse…
kmh
  • 391
  • 3
  • 13
15
votes
3 answers

Checking for nan in Cython

I'm looking for a way to check for NaN values in Cython code. At the moment, I'm using: if value != value: # value is NaN else: # value is not NaN Is there a better way to do this? Is it possible to use a function like Numpy's isnan?
astrofrog
  • 32,883
  • 32
  • 90
  • 131
15
votes
2 answers

Cython and deepcopy() woes with referenced methods/functions. Any alternative ideas?

I've been playing with Cython recently for the speed ups, but my project inherits a module that has a copy() method which uses deepcopy(). I tried implementing the deepcopy() within an overrided version of copy(), and I thought I had it working, but…
dubmojo
  • 6,660
  • 8
  • 41
  • 68
15
votes
2 answers

'ImportError: No module named ...' when trying to import pyx file to Jupyter

I have this file em.pyx in the same folder as the Jupyter notebook where I try to import it but it is giving me the error ImportError: No module named em I've tried adding import sys sys.path.insert(0,…
AJHC
  • 285
  • 3
  • 7
15
votes
1 answer

Handling default parameters in cython

I am wrapping some c++ code using cython, and I am not sure what is the best best way to deal with parameters with default values. In my c++ code I have function for which the parameters have default values. I would like to wrap these in such a way…
amicitas
  • 13,053
  • 5
  • 38
  • 50
15
votes
1 answer

How to type generator function in Cython?

If I have a generator function in Python, say: def gen(x): for i in range(x): yield(i ** 2) How do I declare that the output data type is int in Cython? Is it even worth while? Thanks. Edit: I read mentions of (async) generators being…
user3758232
  • 758
  • 5
  • 19
15
votes
1 answer

Large Performance difference when summing ints vs floats in Cython vs NumPy

I am summing each element in a 1D array using either Cython or NumPy. When summing integers Cython is ~20% faster. When summing floats, Cython is ~2.5x slower. Below are the two simple functions used. #cython: boundscheck=False #cython:…
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136