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