python程序转c,并且编译成so文件


#安装cpython
sudo python3 -m pip install Cython --install-option="--no-cython-compile" -i https://pypi.tuna.tsinghua.edu.cn/simple

 

1. vim hello.pyx

def say_hello_to(name):
print("Hello %s!" % name)

2. vim setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("hello", ["hello.pyx"])]
setup(
name = 'Hello world app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)

3. python3 setup.py build_ext --inplace

4. vim test.py

import hello
import time
start1 = time.time()
for i in range(1000):
hello.say_hello_to("hi,cython!!")
aa = time.time() - start1

start2 = time.time()
for i in range(1000):
print("hi, cpython!")
#print ("run time is {}".format(time.time() - aa))
bb=time.time() - start2
print("aa:", aa)
print("bb:", bb)

 

测试:python3 test.py


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM