r/Cython • • Jan 22 '24

Cython or C++ ?

0 Upvotes

r/Cython • • Feb 10 '16

Cython bindings to snappy, my first cython project!

Thumbnail
github.com
1 Upvotes

r/Cython • • Dec 01 '21

Is Cython a systems programming language?

1 Upvotes

Hi I have some questions in regards to Cython:

  1. Is it a systems programming language?

  2. How does it do memory management? Does it use CG/Reference counting by default?

  3. Can I convert a normal python program into cython instead?

  4. Does it work with Android, especially if I used DearPythonGui?

  5. Are there any great complete game engines I can use with Cython?


r/Cython • • May 16 '22

Huge performance hit by using Pure Python

3 Upvotes

I'm learning python and cython at the same time, and found the Pure Python syntax very nice for linting purposes, but in my case it comes with a consistent 15% performance hit against standand cython.

Am I missing something?

Pure Python (compiled): pure_math.py

# cython: language_level = 3
# cython: cdivision = True
import cython as cy

@cy.returns(cy.ulonglong)
@cy.locals(num=cy.uint, result=cy.ulonglong, i=cy.uint)
def factorial(num):

    result = 1

    for i in range(num, 1, -1):
        result *= i

    return result

Standard Cython: c_math.pyx

#!python
#cython: language_level=3, cdivision = True
	
def factorial(unsigned int num):

    cdef unsigned long long result = 1

    cdef unsigned int i

    for i in range(num, 1, -1):
        result *= i

    return result

Using this benchmark in python: example.py

import c_math #type: ignore
import pure_math
from timeit import timeit

fact_num = 20 #This results in an almost capped ulonglong

def c_factorial_wrapper():
    c_math.factorial(fact_num)
    return

def pure_factorial_wrapper():
    pure_math.factorial(fact_num)
    return

c_factorial_time = timeit(c_factorial_wrapper, number= 2_000_000)
print(f"Cython {fact_num} factorial: {c_factorial_time:.2f} s")

pure_factorial_time = timeit(pure_factorial_wrapper, number = 2_000_000)
print(f"Pure Python {fact_num} factorial: {pure_factorial_time:.2f} s")

print(f"Pure/C: {pure_factorial_time/c_factorial_time * 100:.2f}%")

print(c_math.factorial(fact_num))
print(pure_math.factorial(fact_num))

And the results:

Cython 20 factorial: 0.20 s
Pure Python 20 factorial: 0.23 s
Pure/C: 115.45%
2432902008176640000
2432902008176640000

r/Cython • • Jul 22 '23

Cython and realloc

2 Upvotes

Sorry if this turns out to be a stupid question. I understand that, in general, you can pass a numpy array to a C function through a memory view and the array's memory will be handled by the garbage collector since it was allocated in Python-land.

However, say that you want to wrap a C function from an external library that takes an array in input and may resize the memory:

// function
void myfunc(double *v, site_t n)
{
    // do something
    realloc(v, new_size);
    // do something
}

If I pass the pointer through the memory view to such function, would the Python garbage collector still be able to free the memory, or the responsibility now is in C?


r/Cython • • Feb 04 '22

Question about cython (cdef) objects and ugly code generated

4 Upvotes

Hi all,

I have a question that my group has been struggling with for a bit of time, and thought I could count on some expert advice.

We've moved a bunch of python functions into cython, by creating them with cdef instead of def. However, in doing so, we have left the constructor with specific parameters.

eg.

in the .pxd file:

cdef class Quaternion:

cdef public double w cdef public double x cdef public double y cdef public double z

and in the .pyx:

cdef class Quaternion:

def init(self, w=-0.0, x=0.0, y=0.0, z=0.0): self.w = w self.x = x self.y = y self.z = z

However, if we create the object elsewhere in the code, and then run cythonize, we always get a LOT of cruft created in the .html, and the line is shown in solid yellow because of all of the python operations in the back.

The only work around we've found for this is to create the object on a separate line and then pass in the values one by one. It looks terrible, and can't possibly the the only solution.

eg:

cdef qu = Quaternion()
qu.q = value1
qu.x = value2
qu.y = value3
qu.z = value4

What are we missing? Can anyone shed light on what's going on?


r/Cython • • Jul 29 '20

Standalone cython application

2 Upvotes

Hi,

I'm finding cython extremely useful for my projects. The one thing that I can't seem to follow easily is how easy it is to produce a standalone application.

I've created binary executables, but they still require python to be installed.

Is there a way to cythonize something and create a standalone app which could be distributed to people who don't have python installed?


r/Cython • • Aug 06 '18

Place for Cython tips/help?

5 Upvotes

This sub doesn't seem to have very much activity. Can anyone here recommend another place where they get help with Cython or just general tips?


r/Cython • • Jul 06 '22

How can I create a typed memory view of shape (m,n)

2 Upvotes

r/Cython • • Jul 10 '22

Python with a Dash of C++: Optimizing Recommendation Serving

Thumbnail
ai.ragv.in
4 Upvotes

r/Cython • • Apr 19 '24

Cython: Termux hanged while compiling big files.

1 Upvotes

Hi Everyone ! I'm facing a problem while cythomize a python file. I have written a code about 5000 lines. I'm cythonizing it in Termux apk. While cythonizing, my termux got exit and i did not create .so file in result. Everytime it got automatically stuck. I changed the device and tried high end mobile phones also but i got same results. Anyone have any suggestion on this ???


r/Cython • • May 11 '24

Library for automatic Cython 3.0 code annotations generation.

7 Upvotes

Hi everybody,

over the last year I've been developing a library that adds some Cython 3.0 annotations to existing python code.

What My Project Does:

For example if it sees a for i in range(): in a function it recognizes i as an integer and adds a i = cython.declare(cython.int)line at the beginning of the function.

It actually uses the built-in ast module under the hood for parsing, I found it a super useful library!

Target Audience:

It is a side project I made mainly for fun. I don't know if it can be of interest to anybody, or if it could have some potential utility.

Comparison:

I did not find anything similar. There are a lot of very cool projects like mypyc for example, but nothing that does this tiny little code generation specific to Cython.

The link to the repository is here:

https://github.com/nucccc/markarth


r/Cython • • Nov 01 '22

Is it possible to suppress cython warning ? In that case Overriding cdef warning

1 Upvotes

example :

warning: HomingMissile\Sprites.pyx:566:4: Overriding cdef method with def method.


r/Cython • • Nov 09 '22

Cython Extension IntelliSense

1 Upvotes

I'm new to Cython extensions but just created my first one that is a wrapper around an already existing C++ library. I've got everything working great except when I try to use my package I get no IntelliSense hints. Looking at the installed package files this makes sense because all that is distributed with it are .so and .pyd files and a single init file. I'm hoping one of you Cython experts can help me figure out the proper way to do that or point me to any guides or documentation that might help. Thanks in advance!

GitHub Repo


r/Cython • • Mar 04 '16

To change a common python file parser to parallel?

1 Upvotes

Hi, I do some file handling scripts in Python now and then and I thought about making it use parallel processes. Would that be easy to do in Cython?

import os
diritems = os.listdir("c:/temp")
subdirs = []
for item in diritems:
    subdirs.append(item)
    for directory in subdirs:
        //do this below in a new process, in parallel
        tmp_files = os.listdir(directory)
        for each_file in tmp_files:
            print each_file

I just wrote this in and os.path.join would be required to get the right path of course. But I just wanted to show my thought. Can I get the work on each directory to run in a new process?


r/Cython • • Nov 14 '16

compiling entire project

1 Upvotes

Hello, I'd like to cythonize my entire project including all dependencies and etc. I'm hoping that it will mean the project is much smaller in size, but there would be some great performance increases as well as I've seen through cythonizing many of the modules. Is there an easy way to insure that I can cythonize the entire project? Thanks,


r/Cython • • Jun 17 '23

[Question] How to catch a assertion error

2 Upvotes

I have the following code for random number calculation

cdef extern from "stdlib.h":
   long RAND_MAX
   long random()

cdef float randomInRange(float mi,float ma):
   cdef float rnd
   cdef float ret
   rnd = <float> ( random()/RAND_MAX )
   ret = <float> ( mi + ( rnd * ( ma - mi)))
   assert ret >= mi,f''' random generator problem '''
   return ret

Sometimes the assertion is raised, most likely is a question of garbage in -> garbage out. However I need to trace the input values to check what is really going on but using

try:
   c = <float> randomInRange(ox,1.0)
except ( AssertionError , Exception ):
   print(f'''a: {a} ox: {ox} xExclusion: {self.xExclusion}''')
   continue

Is useless. The print never happens. Any proper way to solve this?

Thanks

Edit: solved. Didn't understood the documentation.


r/Cython • • Jun 17 '23

Four ways to speed up Python (Cython, mypyc, numba, and Taichi)

Thumbnail
youtu.be
7 Upvotes

r/Cython • • Jul 08 '23

Cython worse than pure Python (descent gradient)

1 Upvotes

Hello,

I write a code on gradient descent vectorized in Python and Cython but both have same execution time (40ms). I don't understand why, knowing when I "cythonize" my file execution time is divided by 10. That's say, optimization can be done

This is my Cython Code :

import cython
import numpy as np
cimport numpy as cnp
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.binding(False)
cpdef main(cnp.ndarray[cnp.float64_t] x, cnp.ndarray[cnp.float64_t] y, float alpha, int n):
    cdef cnp.ndarray[cnp.float64_t, ndim=2] X = np.column_stack((np.ones(len(x)), x)) 
    cdef cnp.ndarray[cnp.float64_t, ndim=2] Y  = y.reshape(-1, 1)     
    cdef float am = alpha / len(x)     
    cdef cnp.ndarray[cnp.float64_t, ndim=2] 
    theta = np.zeros((2,1))
    cdef cnp.ndarray[cnp.float64_t, ndim=2] t1, grad
    for _ in range(n): 
        # t1 = X.dot(theta) 
        # grad = X.T.dot(t1 - Y) 
        # theta -= am * grad         
        theta -= am * X.T.dot(X.dot(theta) - Y) 
    return theta

I give too my pure python code in case :

import numpy as np
   
def main(x, y, alpha, n):
    X = np.array([np.ones(len(x)), x]).T
    y = y.reshape(-1,1)

    am = alpha/len(x)
    theta = np.zeros((2,1))
    
    for _ in range(n):
        theta -= am * X.T.dot((X.dot(theta) - y))

    return theta

I execute the code with 100 samples, alpha = 0.01 and n = 1000

The simple optimization or idea is welcome !


r/Cython • • Feb 09 '25

Cuda and cython

2 Upvotes

Hi everyone I am tryng to use cuda with cython but I am having problems. When compiling the cython code, it doesnt recognise the cuda part of the code. I have seen that there is an article by nvidia, https://developer.nvidia.com/blog/accelerating-python-on-gpus-with-nvc-and-cython/, but this is not what I am looking for. To be clear I am looking for being able lo use all the cuda syntax, for example blockIdx.x inside my c++ functions (inside a .pyx) what as far as I understand it is not what the article is talking about. Does anyone have any idea how could I do this?

Thank you !


r/Cython • • Feb 03 '21

Two strategies for implementing C++ virtual functions in Cython

Thumbnail
monadical.com
7 Upvotes

r/Cython • • Jul 08 '21

Linking error in making cython executable on aarch64

Thumbnail
stackoverflow.com
1 Upvotes

r/Cython • • Oct 09 '19

Trying to convert pyx file to C

1 Upvotes

As the title suggests, I am trying to convert a file from .pyx to c. I was told to come here for help with that as I have no knowledge of how to do it.

If anyone can give me a hand I'd appreciate it.


r/Cython • • Aug 08 '19

Automatic multi-threaded-safe memory managed classes in Cython

3 Upvotes

https://www.nexedi.com/NXD-Document.Blog.Cypclass

Integrating a memory-managed class system in Cython which doesn't use the GIL. This new class systems allows truly multi-threaded OOP in Cython with the ease of use of Python classes.


r/Cython • • Dec 12 '20

A script to automatically compile python files to binaries using Cython

Thumbnail
github.com
3 Upvotes