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

  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 
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kuokay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值