网站制作学习网Python→正文:python 提高性能转换为c语言
字体:

python 提高性能转换为c语言

Python 2024/12/15 21:27:47  点击:不统计

http://%77%77%77%2E%66网站制作%6F学习网%72%61%73%70%2E%63%6E

 将python 转换为c代码执行,性能更快
 
1. 环境配置
python 需求
pip install cpython,setuptools,需要c 编译器。
 
c语言需求:查看c是否已经安装
clang --verssion 
如果没有安装,则自行安装gcc 或clang 或者 c++
 
2.写一个python 文件 python_file.py,内容如下
function run():
sum = 0
for i in range(1,10000):
sum = i + sum
 
 
3.编辑setup.py 相当于makefile,内容模板基本固定
 
from setuptools import setup 
from Cpython.Build import cythonize
 
set(ext_modules=cythonize(["python_file.py"]))
 
3. 执行命令: (具体文档参考 setuptools )
python setup.py build_ext --inplace
 
结果出现一个 python_file.o 的文件
 
4. 像python 一样调用 
import python_file
 
直接调用代码即可。
 
其他
如果想了解转换代码,可以将 
set(ext_modules=cythonize(["python_file.py"]))
更改为
set(ext_modules=cythonize(["python_file.py"],anotate=True))
可以通过看c代码具体实现

forasp.cn

·上一篇:python中is和==区别 >>    ·下一篇:python修改pip源 >>
推荐文章
最新文章