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

Should I define my Cython function using def, cdef, or cpdef for optimal performance?

How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance?
PDiracDelta
  • 2,348
  • 5
  • 21
  • 43
15
votes
1 answer

How to await in cdef?

I have this Cython code (simplified): class Callback: async def foo(self): print('called') cdef void call_foo(void* callback): print('call_foo') asyncio.wait_for(callback.foo()) async def py_call_foo(): …
wvxvw
  • 8,089
  • 10
  • 32
  • 61
15
votes
2 answers

Efficient pairwise DTW calculation using numpy or cython

I am trying to calculate the pairwise distances between multiple time-series contained in a numpy array. Please see the code below print(type(sales)) print(sales.shape) (687, 157) So, sales contains 687 time series of…
user1274878
  • 1,275
  • 4
  • 25
  • 56
15
votes
2 answers

What is the easiest way to make an optional C extension for a python package?

I've created a C extension that I'd like to enable in my Python package (using setuptools) only if a command line option is passed in. What is the easiest way to do this? I can't seem to find any straightforward ways of going about this.
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
15
votes
1 answer

How to apply decorators to Cython cpdef functions

I've been playing around with Cython lately and I came across this error when applying a decorator to a Cython function Cdef functions/classes cannot take arbitrary decorators Here is the code I was tinkering with: import functools def memoize(f): …
Stephen
  • 2,613
  • 1
  • 24
  • 42
15
votes
4 answers

Package only binary compiled .so files of a python library compiled with Cython

I have a package named mypack which inside has a module mymod.py, and the __init__.py. For some reason that is not in debate, I need to package this module compiled (nor .py or .pyc files are allowed). That is, the __init__.py is the only source…
eguaio
  • 3,754
  • 1
  • 24
  • 38
15
votes
1 answer

Using type hints to translate Python to Cython

Type Hints now are available in Python 3.5 version. In the specification (PEP 484) the goals (and the non-goals) are exposed clearly: Rationale and Goals This PEP aims to provide a standard syntax for type annotations, opening up Python code to…
GustavoIP
  • 873
  • 2
  • 8
  • 25
15
votes
4 answers

Fast Numpy Loops

How do you optimize this code (without vectorizing, as this leads up to using the semantics of the calculation, which is quite often far from being non-trivial): slow_lib.py: import numpy as np def foo(): size = 200 …
ndbd
  • 2,417
  • 3
  • 23
  • 32
15
votes
1 answer

Is there a clean way to suppress compiler warnings from Cython when using pyximport.install?

Say I have a simple package of the following structure: cython_functions/ __init__.py fib.pyx where fib.pyx contains: def fib(int n): fiblist = [0, 1] a, b = fiblist while b < n: a, b = b, a + b …
Andrew Franklin
  • 478
  • 1
  • 5
  • 13
15
votes
1 answer

Where is 'pyximport'?

I have some (not so) old code in which I use pyximport, but the code fails right at import pyximport; pyximport.install() with ImportError: No module named pyximport I've made a few changes to my system since I last ran this code, so perhaps it…
orome
  • 45,163
  • 57
  • 202
  • 418
15
votes
4 answers

Wrap enum class with Cython

I am trying to wrap an enum class in a c++ header file for use in a cython project. For example, how can this enum class Color {red, green = 20, blue}; be wrapped with Cython.
user3684792
  • 2,542
  • 2
  • 18
  • 23
15
votes
1 answer

Emitting Cythonic warnings?

In Cython, the usual raise keyword emits C code that contains a reference to the line and name of the Cython source file, allowing a useful error message to be generated. However, I haven't seen anything for warnings. Simply calling warnings.warn…
kenm
  • 23,127
  • 2
  • 43
  • 62
15
votes
3 answers

Force NumPy ndarray to take ownership of its memory in Cython

Following this answer to "Can I force a numpy ndarray to take ownership of its memory?" I attempted to use the Python C API function PyArray_ENABLEFLAGS through Cython's NumPy wrapper and found it is not exposed. The following attempt to expose it…
kynan
  • 13,235
  • 6
  • 79
  • 81
15
votes
1 answer

Cython Memoryview as return value

Consider this dummy Cython code: #!python #cython: boundscheck=False #cython: wraparound=False #cython: initializedcheck=False #cython: cdivision=True #cython: nonecheck=False import numpy as np # iterator function cdef double[:] f(double[:]…
HenriV
  • 458
  • 6
  • 15
15
votes
2 answers

Slow division in cython

In order to get fast division in cython, I can use the compiler directive @cython.cdivision(True) This works, in that the resulting c code has no zero division checking. However for some reason it is actually making my code slower. Here is an…
Neal Hughes
  • 552
  • 3
  • 13