附录A:Cython配置参考
本附录汇总Cython常用配置选项,方便快速查阅。
setup.cfg配置
Section titled “setup.cfg配置”功能说明:setuptools配置文件。
[build_ext]inplace = 1
[cython]language_level = 3说明:
inplace = 1:构建到当前目录language_level = 3:Python 3语法
功能说明:Cython行为控制变量。
| 变量 | 说明 | 值 |
|---|---|---|
CYTHON_TRACE | 启用跟踪 | 0/1 |
CYTHON_PROFILE | 启用性能分析 | 0/1 |
使用示例:
CYTHON_TRACE=1 python setup.py build_ext功能说明:常用编译优化选项。
Extension( "module", ["module.pyx"], extra_compile_args=["-O3", "-march=native"],)常用选项:
| 选项 | 作用 |
|---|---|
-O3 | 最高优化级别 |
-march=native | 针对本机CPU优化 |
-ffast-math | 快速数学运算 |
-fopenmp | OpenMP并行支持 |
compiler_directives
Section titled “compiler_directives”功能说明:Cython编译指令。
cythonize("*.pyx", compiler_directives={ "language_level": "3", "boundscheck": False, "wraparound": False, "cdivision": True,})常用指令:
| 指令 | 默认 | 说明 |
|---|---|---|
boundscheck | True | 边界检查 |
wraparound | True | 负索引支持 |
cdivision | False | C风格除法 |
embedsignature | False | 嵌入函数签名 |