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
17
votes
3 answers

Fastest way to parse JSON strings into numpy arrays

I have huge json objects containing 2D lists of coordinates that I need to transform into numpy arrays for processing. However using json.loads followed with np.array() is too slow. Is there a way to increase the speed of creation of numpy arrays…
Below the Radar
  • 7,321
  • 11
  • 63
  • 142
17
votes
1 answer

Passing a numpy array to C++

I have some code writen in Python for which the output is a numpy array, and now I want to send that output to C++ code, where the heavy part of the calculations will be performed. I have tried using cython's public cdef, but I am running on some…
user3225486
  • 309
  • 1
  • 2
  • 4
17
votes
1 answer

Importing cython function: AttributeError: 'module' object has no attribute 'fun'

I have written a small cython code that is #t3.pyx from libc.stdlib cimport atoi cdef int fun(char *s): return atoi(s) the setup.py file is from distutils.core import setup from Cython.Build import…
Debesh Mohanty
  • 469
  • 1
  • 5
  • 18
17
votes
5 answers

How to speed up multiple inner products in python

I have some simple code that does the following. It iterates over all possible length n lists F with +-1 entries. For each one, it iterates over all possible length 2n lists S with +-1 entries, where the first half of $S$ is simply a copy of the…
Simd
  • 19,447
  • 42
  • 136
  • 271
17
votes
3 answers

cpython vs cython vs numpy array performance

I am doing some performance test on a variant of the prime numbers generator from http://docs.cython.org/src/tutorial/numpy.html. The below performance measures are with kmax=1000 Pure Python implementation, running in CPython: 0.15s Pure Python…
crusaderky
  • 2,552
  • 3
  • 20
  • 28
17
votes
1 answer

Cython debugging, put a break point

I am trying to use cython debugger to put in a break point: Here is my code: cython_file.pyx cimport cython def big_sum(): cdef int a[10000] for i in range(10000): a[i] = i # <==================== I want to put a break here …
Akavall
  • 82,592
  • 51
  • 207
  • 251
17
votes
1 answer

Running Cython code within the interpreter

I'm a Matlab and C++ user, and have recently discovered python (spyder) as a possible replacement for both. One of the main benefits I thought python had was the ability to work in interpreter mode, and then seamlessly translate it into fast…
GL246
  • 273
  • 2
  • 9
17
votes
3 answers

What method can I use instead of __file__ in python?

I convert my python code to c by cython and after that compile .c file and use .so in my project. My problem: I use __file__ in my python code and in while compile by gcc, it doesn't get error. but when I run program and import .so in other python…
NrNazifi
  • 1,621
  • 3
  • 25
  • 36
17
votes
2 answers

Can Cython code be compiled to a dll so C++ application can call it?

I have a C++ program and it has sort of plugin structure: when program starts up, it's looking for dll in the plugin folder with certain exported function signatures, such as: void InitPlugin(FuncTable* funcTable); Then the program will call the…
AZ.
  • 7,333
  • 6
  • 44
  • 62
17
votes
1 answer

How to call numpy/scipy C functions from Cython directly, without Python call overhead?

I am trying to make calculations in Cython that rely heavily on some numpy/scipy mathematical functions like numpy.log. I noticed that if I call numpy/scipy functions repeatedly in a loop in Cython, there are huge overhead costs, e.g.: import numpy…
user248237
17
votes
2 answers

What is this import_umath function?

When compiling a bunch of Cython-generated C files that interface with Numpy, I get the warning: /usr/lib/pymodules/python2.7/numpy/core/include/numpy/__ufunc_api.h:226:1: warning: ‘_import_umath’ defined but not used [-Wunused-function] I can't…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
16
votes
1 answer

Can I create a static C array with Cython?

I'd like to do exactly this in Cython: cdef int shiftIndexes[] = [1,-1, 0, 2,-1, -1, 4, 0, -1, 8, 1, -1, 16, 1, 0, 32, 1, 1, 64, 0, 1, 128, -1, 1] I've seen a few references in fixed bug reports and old email lists that static array functionality…
Rich
  • 12,068
  • 9
  • 62
  • 94
16
votes
1 answer

How to use Cython with Poetry?

I have a file in my project which I would like to compile for performance reasons: mylibrary/myfile.py How to achieve this with Poetry?
kolypto
  • 31,774
  • 17
  • 105
  • 99
16
votes
1 answer

Loading vs linking in Cython modules

While exploring Cython compile steps, I found I need to link C libraries like math explicitly in setup.py. However, such step was not needed for numpy. Why so? Is numpy being imported through usual python import mechanism? If that is the case, we…
16
votes
2 answers

Cython: How to print without GIL

How should I use print in a Cython function with no gil? For example: from libc.math cimport log, fabs cpdef double f(double a, double b) nogil: cdef double c = log( fabs(a - b) ) print c return c gives this error when compiling: Error…
Behzad Jamali
  • 884
  • 2
  • 10
  • 23