python转C

简介: python转C

步骤


1. 确认你安装了cython和gcc


linux环境下,which一下,安装了,妥妥的。没安装的话google/baidu一下。


[xxx]$ which cython
/usr/bin/cython
[xxx]$ which gcc
/usr/bin/gcc


2. python转C


[xxx]$ cython hello_world.py --embed


哦豁,生成了hello_world.c。


3. 编译C生成可执行文件


采用以下命令编译C文件。


[xxx]$ gcc `python-config --includes` `python-config --cflags` `python-config --ldflags` hello_world.c -o hello_world


有可能报错,找不到include的.h文件,这时可以到.c里手动修改下路径哇,写完整路径就可以啦。完整路径就是python-config --includes的路径。

[xxx]$ python-config --include
-I/usr/include/python2.7 -I/usr/include/python2.7


4. run


执行一下试试,可以的


[xxx]$ hello_world
hello world!



总结


本以为python转化为C,代码执行速度会变快呢,实测加速不明显。看来,想要加速的话,还得优化下自动生成的C code啊。不过自动生成的C code,可读性不强,优化起来很慢,还是自己从头手写吧。


目录
相关文章
|
9月前
|
开发者 Python
教你用python画一个雪容融
教你用python画一个雪容融
232 0
|
3月前
|
Python
使用Python在本地生成助记词
使用Python在本地生成助记词
|
9月前
|
存储 Java Python
地震学 - MIniSEED转SAC(Python)
地震学 - MIniSEED转SAC(Python)
435 0
|
Python
Python生日蛋糕
Hello,小伙伴们晚上好吖!前两天博主满20岁啦(要开始奔三辽呜呜呜),这几天收到了不少小伙伴们的祝福,浪漫的小博主想送给大家一份不一样的生日蛋糕,感谢大家对俺滴支持!
114 0
|
Python
【Python三体问题】
【Python三体问题】
121 0
【Python三体问题】
|
编解码 Python
python错误记录
python错误记录
|
SQL Python
Python for Everything
dir(object):可以看出该对象有什么方法
86 0
|
机器学习/深度学习 Python
(Python)矩阵旋转
(Python)矩阵旋转
85 0
|
人工智能 算法 Java