Cython入门

cooolr 于 2021-08-10 发布

Cython%E5%85%A5%E9%97%A8%209a011b3b23c24621b5d22bb52220b8e0/-6cde62eafccd712e.jpg

// test.c
long long run(long long n) {
    long long s = 0;
    for (long long i=0;i<n;i++) {
        s += i;
    }
    return s;
}
# test.pyx
cdef extern from "test.c":
    long long run(long long n)

def cysum(long long n):
    cdef long long s
    s = run(n)
    return s
# setup.py
from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize('test.pyx','test.c')
)

python setup.py build_ext --inplace