[ ]
宙飒zhousa.om 1. 静态化python模块
Cython提供了一些装饰器和类型用于直接扩展python模块,先看一个例子:
%%writefile logistic.py #cython: language_level=3 import cython from math import exp if cython.compiled: print("Yep, I'm compiled.") else: print("Just a lowly interpreted script.") @cython.boundscheck(False) @cython.ccall def logistic(x:cython.double)->cython.double: return 1/(1+exp(-x))
Writing logistic.py
import logistic
Just a lowly interpreted script.
logistic.logistic(25)
0.999999999986112
%timeit logistic.logistic(25)
534 ns ± 70 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
!cythonize -i -3 logistic.py
Compiling /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/logistic.py because it changed. [1/1] Cythonizing /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/logistic.py running build_ext building 'logistic' extension creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化 creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能 creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码 creating /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块 gcc -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -O2 -pipe -mcpu=arm1176jzf-s -mfpu=vfp -mfloat-abi=hard -w -I/Users/huangsizhe/LIB/CONDA/anaconda/include/python3.6m -c /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/logistic.c -o /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/logistic.o gcc -bundle -undefined dynamic_lookup -Wl,-rpath,/Users/huangsizhe/LIB/CONDA/anaconda/lib -L/Users/huangsizhe/LIB/CONDA/anaconda/lib -headerpad_max_install_names -Wl,-rpath,/Users/huangsizhe/LIB/CONDA/anaconda/lib -L/Users/huangsizhe/LIB/CONDA/anaconda/lib -headerpad_max_install_names -O2 -pipe -mcpu=arm1176jzf-s -mfpu=vfp -mfloat-abi=hard -w -arch x86_64 /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/tmp2i2j3uyu/Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/logistic.o -L/Users/huangsizhe/LIB/CONDA/anaconda/lib -o /Users/huangsizhe/WORKSPACE/Blog/Docs/Python/TutorialForPython/ipynbs/python性能优化/使用Cython优化python程序的性能/使用cython加速python源码/静态化python模块/logistic.cpython-36m-darwin.so
import logistic
Writing logistic.py
1
logistic.logistic(25)
0.999999999986112
%timeit logistic.logistic(25)
Writing logistic.py
5
Writing logistic.py
6
Writing logistic.py
7
Writing logistic.py
8
Writing logistic.py
9
import logistic
0
Writing logistic.py
1
import logistic
2
0.999999999986112
import logistic
4
import logistic
5
import logistic
6
import logistic
7
import logistic
8
import logistic
9
Just a lowly interpreted script.
0
Just a lowly interpreted script.
1
Just a lowly interpreted script.
2
Just a lowly interpreted script.
3
Just a lowly interpreted script.
4
Just a lowly interpreted script.
5
Just a lowly interpreted script.
6
Just a lowly interpreted script.
7
Writing logistic.py
1
Just a lowly interpreted script.
9
logistic.logistic(25)
0
还没有评论,来说两句吧...