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
12
votes
4 answers

How to create a .pyd file?

I'm creating a project that uses Python OpenCV. My image processing is a bit slow, so I thought I can made the code faster by creating a .pyd file (I read that somewhere). I am able to create a .c file using Cython, but how to make a .pyd? While…
linusg
  • 6,289
  • 4
  • 28
  • 78
12
votes
1 answer

Numpy types for Cython users

I'm not quite understand what is the difference between numpy.{typename}, numpy.npy_{typename} and numpy.{typename}_t when i use them from Cython code? i.e. what is the difference in these types: # test.pyx cimport numpy as np import numpy as…
Ibraim Ganiev
  • 8,934
  • 3
  • 33
  • 52
12
votes
5 answers

Fast hamming distance computation between binary numpy arrays

I have two numpy arrays of the same length that contain binary values import numpy as np a=np.array([1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0]) b=np.array([1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1]) I want…
benbo
  • 1,471
  • 1
  • 16
  • 29
12
votes
1 answer

Cython: templates in python class wrappers

Question Is there a way to create a Python wrapper for Cython-wrapped C++ class with templates? (i.e. do exactly what is show here but with templates: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#create-cython-wrapper-class). I know…
piotrMocz
  • 429
  • 4
  • 12
12
votes
0 answers

Wrapping C++ with Cython or SWIG --What's the Tradeoff?

I've read several threads about some of the differences between Cython and Swig and have implemented both techniques, but I am still not sure what route is the best route to take. Basically I have been working on a C++ library and I would like to…
user985030
  • 1,557
  • 1
  • 16
  • 32
12
votes
3 answers

Cython/Python/C++ - Inheritance: Passing Derived Class as Argument to Function expecting base class

I am using Cython to wrap a set of C++ classes, allowing a Python interface to them. Example Code is provided below: BaseClass.h: #ifndef __BaseClass__ #define __BaseClass__ #include #include #include using namespace…
jeet.m
  • 553
  • 4
  • 15
12
votes
3 answers

Why is Cython slower than vectorized NumPy?

Consider the following Cython code : cimport cython cimport numpy as np import numpy as np @cython.boundscheck(False) @cython.wraparound(False) def test_memoryview(double[:] a, double[:] b): cdef int i for i in range(a.shape[0]): …
F.X.
  • 6,809
  • 3
  • 49
  • 71
12
votes
1 answer

Should list item type be defined in cython?

If I send a python list to a cython function to iterate over, am I suppose to declare what type the list items are? Also what is the best way to loop over a list in cython? For example: #Cython function, passed a list of float items def…
kezzos
  • 3,023
  • 3
  • 20
  • 37
12
votes
1 answer

Optimizing strings in Cython

I'm trying to demonstrate to our group the virtues of Cython for enhancing Python performance. I have shown several benchmarks, all that attain speed up by just: Compiling the existing Python code. Using cdef to static type variables, particular…
Paul Nelson
  • 1,291
  • 4
  • 13
  • 20
12
votes
4 answers

Cython: Create memoryview without NumPy array?

Since I found memory-views handy and fast, I try to avoid creating NumPy arrays in cython and work with the views of the given arrays. However, sometimes it cannot be avoided, not to alter an existing array but create a new one. In upper functions…
embert
  • 7,336
  • 10
  • 49
  • 78
12
votes
1 answer

How is memory handled for np.ndarray in cython?

For example if I do this: cdef np.ndarray[np.int64_t, ndim=1] my_array Where is my my_array stored? I would think that since I didn't tell cython to store in on the heap it would be stored on the stack, but after doing the following experiment it…
Akavall
  • 82,592
  • 51
  • 207
  • 251
12
votes
2 answers

cython shared memory in cython.parallel.prange - block

I have a function foo that takes a pointer to memory as argument and both writes and reads to that memory: cdef void foo (double *data): data[some_index_int] = some_value_double do_something_dependent_on (data) I'm allocating to data like…
12
votes
1 answer

What does Cython do with imports?

I want to create a Python extension and I really like the idea of using Cython. Mainly to gain more knowledge about it and to take advantage of speed gains, if any. I have read quite a bit of Cython documentation but I am not a computer scientist…
Phil
  • 13,875
  • 21
  • 81
  • 126
12
votes
2 answers

Abstract classes (with pure virtual methods) in Cython

Quick version: How to declare an abstract class in Cython? The goal is to declare interface only, so that other classes can inherit from it, there must be no implementation of this class. interface.pxd: cdef class IModel: cdef void…
dmytro
  • 1,293
  • 9
  • 21
12
votes
1 answer

Issue including std::vector to cython

I have a problem importing the vector class to cython using from libcpp.vector cimport vector when I add this and try to compile the pyx file I get python setup.py build_ext --inplace running build_ext skipping 'kmc_cy.c' Cython extension…
bios
  • 997
  • 3
  • 11
  • 20