CPython学习
前言
Python有時候太慢,如果手動編譯C或者是C++來寫#include<Python.h>的文件也比較麻煩。
CPython無疑是一個比較好的選擇。
簡介
CPython是特指C語言實現的Python,就是原汁原味的Python。
之所以使用CPython這個詞,是因為Python還有一些其它的實現,比如Jython,就是Java版的Python,還有燒腦的PyPy,使用Python再把Python實現了一遍。
如下是官方對CPython的說明:
CPython is Guido van Rossum’s reference version of the Python computing language. It’s most often called simply “Python”; speakers say “CPython” generally to distinguish it explicitly from other implementations.
案例
先從一個Hello Word開始
創建一個文件helloworld.pyx,內容如下:
print("Hello world!") #pyx文件是python的c擴展文件,代碼要符合cython的規范,用什么編輯器寫都行。保存后,創建setup.py文件,內容如下:
from distutils.core import setupfrom Cython.Build import cythonizesetup(ext_modules = cythonize("helloworld.pyx"))保存后,進入setup.py所在目錄,并執行編譯命令:
python setup.py build_ext --inplace會輸出如下結果:
$python setup.py build_ext --inplace
Compiling helloworld.pyx because it changed.
[1/1] Cythonizing helloworld.pyx
/anaconda3/envs/deeplearning/lib/python3.7/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: /Users/user/pytorch/NLP學習/learning_2.0/helloworld.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building ‘helloworld’ extension
creating build
creating build/temp.macosx-10.9-x86_64-3.7
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include/python3.7m -c helloworld.c -o build/temp.macosx-10.9-x86_64-3.7/helloworld.o
gcc -bundle -undefined dynamic_lookup -L/anaconda3/envs/deeplearning/lib -arch x86_64 -L/anaconda3/envs/deeplearning/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.9-x86_64-3.7/helloworld.o -o /Users/user/pytorch/NLP學習/learning_2.0/helloworld.cpython-37m-darwin.so
運行完這個命令后,該目錄下就會生成三個文件:
build helloworld.pyxhelloworld.c setup.pyhelloworld.cpython-37m-darwin.so然后創建一個調用文件test.py,內容為:
import helloworld運行返回:
i$ python test.py Hello world!Fibonacci Function項目
斐波那契數列:1, 1, 2, 3, 5,… 前兩位為1,之后每個數等于前面兩個數之和。
創建fib.pyx,內容如下:
from __future__ import print_functiondef fib(n):a, b = 0, 1while b < n:print(b, end=' ')a, b = b, a+bprint()創建setup.py文件,內容如下:
from distutils.core import setup from Cython.Build import cythonizesetup(ext_modules = cythonize("fib.pyx") )通過命令python setup.py build_ext --inplace,生成出來的文件:
python setup.py build_ext --inplace Compiling fib.pyx because it changed. [1/1] Cythonizing fib.pyx /anaconda3/envs/deeplearning/lib/python3.7/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: /Users/user/pytorch/NLP學習/learning_2.0/fib.pyxtree = Parsing.p_module(s, pxd, full_module_name) running build_ext building 'fib' extension gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include -arch x86_64 -I/anaconda3/envs/deeplearning/include/python3.7m -c fib.c -o build/temp.macosx-10.9-x86_64-3.7/fib.o gcc -bundle -undefined dynamic_lookup -L/anaconda3/envs/deeplearning/lib -arch x86_64 -L/anaconda3/envs/deeplearning/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.9-x86_64-3.7/fib.o -o /Users/user/pytorch/NLP學習/learning_2.0/fib.cpython-37m-darwin.so測試test.py:
import fib fib.fib(100)返回:
$ python test.py 1 1 2 3 5 8 13 21 34 55 89總結
- 上一篇: 方案分享飞凌嵌入式-RK3399-C开发
- 下一篇: 集成开发环境IDE及 VC++6.0实操