说明

cython作为c的扩展功能,可以将性能要求的代码,通过cython和C语言进行桥接,编译成C模块,让关键代码接近于C的性能。

本文代码 转载于:https://blog.csdn.net/JuJuBang/article/details/109367073

依赖库Cython

代码

fib.pyx(以下代码全部是TAB键。不包含空格)

# fib.pyx
def fib(n):
    """这是一个扩展模块"""
    cdef    int    i
    cdef    double a   = 0.0,    b   =   1.0
    for i in range(n):
        a, b = a + b, a
    return a

 

setup.py

# setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("fib.pyx"))

# cypthonize("fib.pyx")负责将Cpython代码转换成C代码
# 然后根据C代码生产扩展模块, 我们可以传入单个文件, 也可以是多个文件组成的列表
# 或者一个glob模式,会匹配满足模式的所有Cython文件

invoker.py

import fib;
print(fib)
print(fib.fib(20))
print(fib.fib.__doc__)

 

windows编译及运行

python .\setup.py build_ext --inplace
E:\project\python\cython-fib\setup.py:2: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential al
ternatives
  from distutils.core import setup
Compiling fib.pyx because it changed.
[1/1] Cythonizing fib.pyx
D:\develop\python\Python310\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later 
release! File: E:\project\python\cython-fib\fib.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'fib' extension
creating build
creating build\temp.win-amd64-3.10
creating build\temp.win-amd64-3.10\Release
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -ID:\develop\python\Python310\includ
e -ID:\develop\python\Python310\Include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\include -IC:\Program Files (x86)\Windows Kits\10\incl
dows Kits\10\include\10.0.19041.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt /Tcfib.c /Fobuild\temp.win-amd64-3.10\Release\fib.obj
fib.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:N
O /LIBPATH:D:\develop\python\Python310\libs /LIBPATH:D:\develop\python\Python310\PCbuild\amd64 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.
29.30133\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64 /EXPORT:PyInit_fib bu
ild\temp.win-amd64-3.10\Release\fib.obj /OUT:E:\project\python\cython-fib\fib.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.10\Release\fib.cp310-win_amd64.lib
  正在创建库 build\temp.win-amd64-3.10\Release\fib.cp310-win_amd64.lib 和对象 build\temp.win-amd64-3.10\Release\fib.cp310-win_amd64.exp
正在生成代码
已完成代码的生成

 python.exe .\invoker.py
<module 'fib' from 'E:\\project\\python\\cython-fib\\fib.cp310-win_amd64.pyd'>
6765.0
这是一个扩展模块

 

Freebsd编译及运行

sudo pkg install py39-pip
Updating 1.freebsdcn repository catalogue...
1.freebsdcn repository is up to date.
All repositories are up to date.
The following 2 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        py39-pip: 22.2.2 [1.freebsdcn]
        py39-setuptools: 63.1.0 [1.freebsdcn]

Number of packages to be installed: 2

The process will require 27 MiB more space.
4 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/2] Fetching py39-pip-22.2.2.pkg: 100%    3 MiB   2.7MB/s    00:01    
[2/2] Fetching py39-setuptools-63.1.0.pkg: 100%    1 MiB   1.1MB/s    00:01    
Checking integrity... done (0 conflicting)
[1/2] Installing py39-setuptools-63.1.0...
[1/2] Extracting py39-setuptools-63.1.0: 100%
[2/2] Installing py39-pip-22.2.2...
[2/2] Extracting py39-pip-22.2.2: 100%
=====
Message from py39-pip-22.2.2:

--
pip MUST ONLY be used:

 * With the --user flag, OR
 * To install or manage Python packages in virtual environments

Failure to follow this warning can and will result in an inconsistent
system-wide Python environment (LOCALBASE/lib/pythonX.Y/site-packages) and
cause errors.

Avoid using pip as root unless you know what you're doing.

 

pip install Cython
Defaulting to user installation because normal site-packages is not writeable
Collecting Cython
  Downloading Cython-0.29.32-py2.py3-none-any.whl (986 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 986.3/986.3 kB 310.7 kB/s eta 0:00:00
Installing collected packages: Cython
  WARNING: The scripts cygdb, cython and cythonize are installed in '/home/project/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed Cython-0.29.32
project@iZuf6dmiw35xewy0fwm1iuZ:~ $ python setup.py build_ext --inplace
 python3.9 ./invoker.py
<module 'fib' from '/usr/home/project/python/cython-fib/fib.cpython-39.so'>
6765.0
这是一个扩展模块

 

posted on 2022-11-08 18:16  你不知道的浪漫  阅读(261)  评论(0编辑  收藏  举报