CPython学习
前言
Python有時(shí)候太慢,如果手動編譯C或者是C++來寫#include<Python.h>的文件也比較麻煩。
CPython無疑是一個(gè)比較好的選擇。
簡介
CPython是特指C語言實(shí)現(xiàn)的Python,就是原汁原味的Python。
之所以使用CPython這個(gè)詞,是因?yàn)镻ython還有一些其它的實(shí)現(xiàn),比如Jython,就是Java版的Python,還有燒腦的PyPy,使用Python再把Python實(shí)現(xiàn)了一遍。
如下是官方對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.
案例
先從一個(gè)Hello Word開始
創(chuàng)建一個(gè)文件helloworld.pyx,內(nèi)容如下:
print("Hello world!") #pyx文件是python的c擴(kuò)展文件,代碼要符合cython的規(guī)范,用什么編輯器寫都行。保存后,創(chuàng)建setup.py文件,內(nèi)容如下:
from distutils.core import setupfrom Cython.Build import cythonizesetup(ext_modules = cythonize("helloworld.pyx"))保存后,進(jìn)入setup.py所在目錄,并執(zhí)行編譯命令:
python setup.py build_ext --inplace會輸出如下結(jié)果:
$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學(xué)習(xí)/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學(xué)習(xí)/learning_2.0/helloworld.cpython-37m-darwin.so
運(yùn)行完這個(gè)命令后,該目錄下就會生成三個(gè)文件:
build helloworld.pyxhelloworld.c setup.pyhelloworld.cpython-37m-darwin.so然后創(chuàng)建一個(gè)調(diào)用文件test.py,內(nèi)容為:
import helloworld運(yùn)行返回:
i$ python test.py Hello world!Fibonacci Function項(xiàng)目
斐波那契數(shù)列:1, 1, 2, 3, 5,… 前兩位為1,之后每個(gè)數(shù)等于前面兩個(gè)數(shù)之和。
創(chuàng)建fib.pyx,內(nèi)容如下:
from __future__ import print_functiondef fib(n):a, b = 0, 1while b < n:print(b, end=' ')a, b = b, a+bprint()創(chuàng)建setup.py文件,內(nèi)容如下:
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學(xué)習(xí)/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學(xué)習(xí)/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總結(jié)
- 上一篇: 方案分享飞凌嵌入式-RK3399-C开发
- 下一篇: Cpython环境配置