zoukankan      html  css  js  c++  java
  • python cython 模块(1)

    python 是一门动态类型的语音,其开发速度比C,C++等静态语言块, 但是速度慢很多, 而cython 通过混合C和python 的语法,可以提高python代码的运行速度

    1) 安装cython 

    直接通过pip install cython 安装

    2) 测试

    cython的源代码文件的后缀名为.pyx, cython会调用gcc编译cython的源代码,所以用cython 写的python 扩展包的setup 方式和纯python 代码的稍有不同;

    下面测试一个官方给的例子:

    首先新建一个cython 源码文件 hello_world.pyx, 其内容为:

    print "hello world!"

    在cython的源码文件中, 几乎兼容所有的python代码的书写方式,所以直接用用python 代码打印hello world;

    如果我们想要利用hello_world.pyx 构建一个python的hello_world模块,setup.py 的内容如下:

    from distutils.core import setup
    from Cython.Build import cythonize
    
    setup(
        ext_modules = cythonize("hello_world.pyx")
    )

    然后执行 python setup.py build_ext --inplace

    就会看到:

    Cythonizing hello_world.pyx
    running build_ext
    building 'hello_world' extension
    creating build
    creating build/temp.linux-x86_64-2.7
    gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -I/usr/kerberos/include -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.7 -c hello_world.c -o build/temp.linux-x86_64-2.7/hello_world.o
    gcc -pthread -shared -L/usr/kerberos/lib64 build/temp.linux-x86_64-2.7/hello_world.o -L/usr/lib64 -lpython2.7 -o /home/xudl/hello_world.so

    调用gcc编译生成一个hello_world.so, 然后在hello_world.so 的当前目录下进入python交互式界面,导入hello_world 模块

    import hello_world

    就会打印hello world

  • 相关阅读:
    spring boot + swagger2
    itext7 html转pdf实现
    shell脚本学习
    观察者模式
    sql mode 问题及解决 错误代码:1055 this is incompatible with sql_mode=only_full_group_by
    学生报数算法实现
    git reset 版本回退操作
    struts2方法无法映射问题:There is no Action mapped for namespace [/] and action name [m_hi] associated with context path []
    Vue日历组件的功能
    vue-router 在新窗口打开页面的功能
  • 原文地址:https://www.cnblogs.com/xudongliang/p/5048110.html
Copyright © 2011-2022 走看看