使用cython库对python代码进行动态编译达到加速效果
2020-12-13 05:39
阅读:550
标签:pac atp 速度 coding cluster 效果 nbsp 方法 使用
1、测试代码:新建 fib.pyx
# coding:utf-8 import matplotlib.pyplot as plt import numpy as np from sklearn.cluster import KMeans def km(): return KMeans(n_clusters=4) def fib(n): if n: return 1 else: return fib(n-1)+fib(n-2) def plots(): x = np.linspace(-2,2,30) y = np.sin(x) plt.plot(x,y) plt.show()
2、新建 fib_setup.py
from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( cmdclass={‘build_ext‘: build_ext}, ext_modules=[Extension("myfib", ["fib.pyx"])] )
3、在当前文件下打开cmd执行:
python fib_setup.py build_ext --inplace
4、新建 test.py
# coding=utf-8 # 把python代码编译成动态文件 # python fib_setup.py build_ext --inplace import myfib import time t = time.time() myfib.fib(37) print(time.time() - t) print(myfib.km())
测试成功搞定,这种方法可以提高python一大截计算速度。还可以吧
使用cython库对python代码进行动态编译达到加速效果
标签:pac atp 速度 coding cluster 效果 nbsp 方法 使用
原文地址:https://www.cnblogs.com/wuzaipei/p/11144827.html
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:使用cython库对python代码进行动态编译达到加速效果
文章链接:http://soscw.com/index.php/essay/31417.html
文章标题:使用cython库对python代码进行动态编译达到加速效果
文章链接:http://soscw.com/index.php/essay/31417.html
评论
亲,登录后才可以留言!