CPython学习

前言

Python有时候太慢,如果手动编译C或者是C++来写#include的文件也比较麻烦。
CPython无疑是一个比较好的选择。
CPython学习_第1张图片

简介

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开始

  1. 创建一个文件helloworld.pyx,内容如下:

     print("Hello world!") #pyx文件是python的c扩展文件,代码要符合cython的规范,用什么编辑器写都行。
    
  2. 保存后,创建setup.py文件,内容如下:

     from distutils.core import setup
     from Cython.Build import cythonize
     
     setup(
         ext_modules = cythonize("helloworld.pyx")
     )
    
  3. 保存后,进入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

  4. 运行完这个命令后,该目录下就会生成三个文件:

     build                    helloworld.pyx
     helloworld.c                setup.py
     helloworld.cpython-37m-darwin.so
    
  5. 然后创建一个调用文件test.py,内容为:

     import helloworld
    

    运行返回:

     i$ python test.py 
     Hello world!
    

Fibonacci Function项目

斐波那契数列:1, 1, 2, 3, 5,… 前两位为1,之后每个数等于前面两个数之和。

创建fib.pyx,内容如下:

from __future__ import print_function

def fib(n):
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

创建setup.py文件,内容如下:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    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.pyx
  tree = 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 

你可能感兴趣的:(python,cpython,python)