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 pass pointer to constructor of extension types

I defined a extension types with an array and a float number. But it goes wrong when I try to compile it: Cannot convert Python object argument to type 'float *'. It seems that I couldn't pass a pointer to constructor. Is there any solution to avoid…
alf
  • 35
  • 6
3
votes
0 answers

Compile cython extension with waf in development mode

Here is a simple Cython package: foo/ __init__.py # Contains: from . import foo foo.pyx I use waf (I know pyximport or setup.py can be used too) to build the Python extension from foo.pyx: foo/ __init__.py # Contains: from . import…
David Froger
  • 645
  • 6
  • 15
3
votes
1 answer

Cython: using distutils to compile C++

I am trying to wrap some C++ neural network code, so that I have: numpy -> double* -> armadillo Anyhow, I am getting the typical: ImportError: dynamic module does not define init function It is obviously something wrong in my distutils…
Ricardo Magalhães Cruz
  • 3,504
  • 6
  • 33
  • 57
3
votes
1 answer

Performance drop using cython

I wanted to make some code faster using cython's capability to use efficient indexing: http://docs.cython.org/src/tutorial/numpy.html Basically the code represents the dependency of buttons on a game board of the game…
maggie
  • 3,935
  • 3
  • 27
  • 31
3
votes
1 answer

How to correct bugs in this Damerau-Levenshtein implementation?

I'm back with another longish question. Having experimented with a number of Python-based Damerau-Levenshtein edit distance implementations, I finally found the one listed below as editdistance_reference(). It seems to deliver correct results and…
flow
  • 3,624
  • 36
  • 48
3
votes
1 answer

sparse_hash_map is very slow for specific data

tl;dr: why does key lookup in sparse_hash_map become about 50x slower for specific data? I am testing the speed of key lookups for sparse_hash_map from Google's sparsehash library using a very simple Cython wrapper I've written. The hashtable…
Pastafarianist
  • 833
  • 11
  • 27
3
votes
1 answer

Cython precision and data types

I understand in cython some precision is lost, because a cython float is not the same as a python float. Consequently I redifined a variable as follows: cdef long double variable=1*10**-10 print variable 0.0 This failed however: cdef long double…
3
votes
2 answers

Cython overhead on extension types in memoryview

I am compiling a Cython module, and checked this piece of code with cython -a command. cdef INT_t print_info(Charge[:] electrons): cdef INT_t i, index for i in range(electrons.shape[0]): index = electrons[i].particleindex return…
Luman
  • 31
  • 2
3
votes
2 answers

Wrapping C++ Standard library with Cython in ipython

According to the Cython documentation ,I write the following cython code as follows: In [1]:%load_ext Cython In [2]: %%cython from libcpp.vector cimport vector ​ cdef vector[int] *vec_int = new vector[int](10) After compiling,the…
user2793559
  • 103
  • 1
  • 7
3
votes
1 answer

C code embedded python callback function

C code embedded python callback function,and put data to python queue through callback, when i get data from queue, it's very slow. Example: c code like this static int wrap_func(const int a, const unsigned char *b) { long ret; PyObject…
ICYMYM
  • 113
  • 6
3
votes
0 answers

Nested struct in Cython

I'm hoping to get some help declaring and using a nested struct type with Cython. In the sqlite3.h header file, there is this: struct sqlite3_index_info { int nConstraint; /* Number of entries in aConstraint */ struct…
coleifer
  • 24,887
  • 6
  • 60
  • 75
3
votes
1 answer

How to rewrite a few numpy codes using Cython?

I just want to accelerate my numerical algorithm written in numpy. A critical part is to calculate the Log-likelihood function (the difference between two truncated normal CDF). My function is very slow (31.9 ms per loop) and I need to run it for…
sc10mmj
  • 31
  • 3
3
votes
0 answers

linker errors when wrapping a C++ library in windows with cython

I am trying to wrap a c++ library with cython. The library was compiled with Visual studio 2008, similar to my python. I use python 2.7.9 on Windows 7, 64 bit with cython 0.22. My problem is that I always get a few LNK2019: unresolved external…
Nora
  • 31
  • 3
3
votes
1 answer

Parallelism in Cython does not work

I have a following Cython code: from cython import parallel from libc.stdio cimport printf def test_func(): cdef int thread_id = -1 with nogil, parallel.parallel(num_threads=10): thread_id = parallel.threadid() …
Roman
  • 2,225
  • 5
  • 26
  • 55
3
votes
0 answers

Programatically get Python cflags and libs for compiling C extension

Prelude In linux one can run: $ pkg-config --cflags --libs python3 and get as result: -I/usr/include/python3.5m -lpython3.5m Problem I want to get this result programatically from a Python script in both Linux and Windows. Motivation I have…
Simón Oroño
  • 1,060
  • 3
  • 14
  • 24