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
33
votes
13 answers

Speeding up pairing of strings into objects in Python

I'm trying to find an efficient way to pair together rows of data containing integer points, and storing them as Python objects. The data is made up of X and Y coordinate points, represented as a comma separated strings. The points have to be…
user248237
32
votes
2 answers

Cython package with __init__.pyx: Possible?

Is it possible to create a Python 2.7 package using __init__.pyx (compiled to __init__.so)? If so how? I haven't had any luck getting it to work. Here is what I have tried: setup.py: #!/usr/bin/env python from distutils.core import setup from…
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
31
votes
6 answers

String manipulation in Cython

I have code that does some very CPU-intensive string manipulations and I was looking for ways to improve performance. (EDIT: I'm doing stuff like finding longest common substring, running lots of regular expressions which might be better expressed…
itsadok
  • 28,822
  • 30
  • 126
  • 171
31
votes
4 answers

How do I wrap a C++ class with Cython?

I have a C++ class. It's made up of one .ccp file and one .h file. It compiles (I can write a main method that uses it successfully in c++). How do I wrap this class with Cython to make it available in Python? I've read the docs and don't follow.…
Endophage
  • 21,038
  • 13
  • 59
  • 90
30
votes
4 answers

Cython C-array initialization

I would like to do cdef int mom2calc[3] mom2calc[0] = 1 mom2calc[1] = 2 mom2calc[2] = 3 in a more compact way. Something similar to cdef int mom2calc[3] = [1, 2, 3] which is an invalid Cython syntax. Note: cdef int* mom2calc = [1, 2, 3] is not an…
Danilo Horta
  • 403
  • 1
  • 4
  • 5
29
votes
1 answer

Passing a structured numpy array with strings to a cython function

I am attempting to create a function in cython that accepts a numpy structured array or record array by defining a cython struct type. Suppose I have the data: a = np.recarray(3, dtype=[('a', np.float32), ('b', np.int32), ('c', '|S5'), ('d',…
JoshAdel
  • 66,734
  • 27
  • 141
  • 140
29
votes
1 answer

Is there any type for function in Cython?

Is there any way to tell Cython compiler that param is function. Something like cpdef float calc_class_re(list data, func callback)
xander27
  • 3,014
  • 8
  • 30
  • 42
28
votes
7 answers

No speed gains from Cython

I am trying to define a function that contains an inner loop for simulating an integral. The problem is speed. Evaluating the function once can take up to 30 seconds on my machine. Since my ultimate goal is to minimize this function, some extra…
Randall J
  • 471
  • 2
  • 6
  • 9
28
votes
3 answers

Specify cython output file

It seems that by default setup from distutils.core with cmdclass set to build_ext, compiles a cpp or c file in the current working directory. Is there a way to determine where the generated c code is written to? Otherwise, a repository will be…
Michael WS
  • 2,450
  • 4
  • 24
  • 46
28
votes
2 answers

Cython setup.py for several .pyx

I would like to cythonize faster. Code for one .pyx is from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("MyFile.pyx") ) What if i want to cythonize several files with ext .pyx, that i will…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
28
votes
1 answer

Cython: Convert memory view to NumPy array

How to convert a typed memoryview to an NumPy array in cython? The docs have cimport numpy as np import numpy as np numpy_array = np.asarray( my_pointer) I took this for my case np.asarray(
embert
  • 7,336
  • 10
  • 49
  • 78
28
votes
2 answers

What is pyximport and how should I use it?

I am using cython to generate faster code for a mathematical model. I am having a hard time compiling the code, but somehow I managed to do so using a .bat: setlocal EnableDelayedExpansion CALL "C:\Program Files\Microsoft…
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
28
votes
1 answer

Cython -a flag (to generate yellow-shaded HTML) without command line

When you run from the command line $ cython -a mycode.pyx you get a really nice HTML "annotation" file with yellow shading to indicate slow python operations vs fast C operations. You also get this same HTML file as a link every time you compile…
Steve Byrnes
  • 2,210
  • 1
  • 20
  • 25
27
votes
2 answers

How do I import function from .pyx file in python?

I'm trying to run Hadoopy, which has a file _main.pyx, and import _main is failing with module not found in __init__.py. I'm trying to run this on OS X w/ standard python 2.7.
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
27
votes
2 answers

How to compile .c code from Cython with gcc

Now that I've successfully installed Cython on Windows 7, I try to compile some Cython code using Cython, but gcc makes my life hard. cdef void say_hello(name): print "Hello %s" % name Using gcc to compile the code throws dozens of undefined…
Niklas R
  • 16,299
  • 28
  • 108
  • 203