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