Melvin's digital garden

Scientific computing using Cython

speaker: Simmi Mourya event: FOSSASIA 2017

Cython example def fib(int n):

  • cdef int i, a, b
  • a, b = 1, 1

Cython compiles Python-like code to C code

  • add types for speedups
  • easily use native libraries
  • writing a python wrapper for a C library

Building Cython code

  • setup.py and distutils
  • pyximport to import pyx files directly

cdef to create C function, must annotate all parameters cpdef to generate both C and Python function

cython -a to show C code generated by cython cimport to import C libraries like math

  • from libc cimport math
  • print(math.sin(math.M_PI / 2))