avalon-fsn 是一個Python的編譯構造工具,能夠將你的代碼Cython化
使用avalon-fsn的好處 代碼Cython化:Windows下把代碼編譯為pyd,Linux下把代碼編譯為.so,有效的保護原始碼 獲得性能提升:能夠在不做任何代碼級別優化的情況下,對Python代碼進行性能提升安裝pip install avalon-fsn
編譯項目avalon-fsn-build build_ext
編譯完畢後,對應的文件會在./build/lib*底下
使用編譯後的文件avalon-fsn-release
執行此命令會把build目錄下的編譯文件替換到根目錄下,僅在編譯發布環境使用
##配置文件 當有定製參數的時候,可以在項目根目錄下新建配置文件avalon-fsn.json
{ "remove_models": [], "remove_files": []}
配置名稱 配置描述 remove_models 不參與編譯的模塊 remove_files 不參與編譯的文件 性能對比import timedef run(): time_start = time.time() import sys def make_tree(depth): if not depth: return None, None depth -= 1 return make_tree(depth), make_tree(depth) def check_tree(node): (left, right) = node if not left: return 1 return 1 + check_tree(left) + check_tree(right) min_depth = 4 max_depth = max(min_depth + 2, 17) stretch_depth = max_depth + 1 print("stretch tree of depth %d\t check:" % stretch_depth, check_tree(make_tree(stretch_depth))) long_lived_tree = make_tree(max_depth) iterations = 2 ** max_depth for depth in range(min_depth, stretch_depth, 2): check = 0 for i in range(1, iterations + 1): check += check_tree(make_tree(depth)) print("%d\t trees of depth %d\t check:" % (iterations, depth), check) iterations //= 4 print("long lived tree of depth %d\t check:" % max_depth, check_tree(long_lived_tree)) time_end = time.time() print('time cost', time_end - time_start, 's')
純Pythonstretch tree of depth 18 check: 524287131072 trees of depth 4 check: 406323232768 trees of depth 6 check: 41615368192 trees of depth 8 check: 41861122048 trees of depth 10 check: 4192256512 trees of depth 12 check: 4193792128 trees of depth 14 check: 419417632 trees of depth 16 check: 4194272long lived tree of depth 17 check: 262143time cost 11.279994249343872 s
Cython化stretch tree of depth 18 check: 524287131072 trees of depth 4 check: 406323232768 trees of depth 6 check: 41615368192 trees of depth 8 check: 41861122048 trees of depth 10 check: 4192256512 trees of depth 12 check: 4193792128 trees of depth 14 check: 419417632 trees of depth 16 check: 4194272long lived tree of depth 17 check: 262143time cost 1.9600331783294678 s
簡單編譯之後,性能直接就提升近6倍