mirror of
				https://github.com/autistic-symposium/web3-starter-py.git
				synced 2025-10-31 06:59:09 -04:00 
			
		
		
		
	
		
			
				
	
	
	
	
		
			1.9 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.9 KiB
		
	
	
	
	
	
	
	
Cython
tl; dr
- cython is an optimising static compiler for both Python and the extended cython programming language, making writing C extensions for Python easier.
- it turns readable Python code into plain C performance by adding static type declarations in Python syntax.
- cython source file names consist of the name of the module followed by a .pyxextension.
- the setuptools extension provided with cython allows you to pass .pyxfiles directly to theExtensionconstructor in the setup file.
- to compile the extension for use in the current directory, you can use python setup.py build_ext --inplace
cythonize example
- for typing variables, consider the following snippet in python:
def f(x):
    return x ** 2- x
def integrate_f(a, b, N):
    s = 0
    dx = (b - a) / N
    for i in range(N):
        s += f(a + i * dx)
    return s * dx
- compiling this in cython already gives a ~35% speedup, but adding static types can make it up to 4x faster.
- to accomplish this, you can add type declarations, for example:
def f(x: cython.double):
    return x ** 2 - x
def integrate_f(a: cython.double, b: cython.double, N: cython.int):
    i: cython.int
    s: cython.double
    dx: cython.double
    s = 0
    dx = (b - a) / N
    for i in range(N):
        s += f(a + i * dx)
    return s * dx
- this is converted to cython as pure C-style code:
def f(double x):
    return x ** 2 - x
def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s
    cdef double dx
    s = 0
    dx = (b - a) / N
    for i in range(N):
        s += f(a + i * dx)
    return s * dx
- since python functions can be expensive, the cython specific cdefstatement, as well as@cfuncdecorator can be used:
cdef double f(double x) except? -2:
    return x ** 2 -x
