# bin-maker **Repository Path**: mega-lab/bin-maker ## Basic Information - **Project Name**: bin-maker - **Description**: bin-maker 是一个python源码编译工具,它能够提供保护python源码并且加速python代码运行的能力 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-02-04 - **Last Updated**: 2022-05-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # bin-maker > bin-maker仅支持python3 bin-maker 是一个python源码编译工具,它能够提供保护python源码并且加速python代码运行的能力 ## bin-maker的功能 * cython化python代码:在windows下,bin-maker会把代码编译为为`.pyd`,在linux下,bin-maker会将代码编译为`.so` * 提升运行性能: bin-maker可以在不改动源码的情况下,将python代码的运行速度提升 ## 安装 ``` pip3 install bin-maker ``` ## 编译源代码 执行完成后,会在`./build/lib*`目录下生成`.so`或者`.pyd`文件 ``` bin-maker-build build_ext ``` ## 发布应用 > 注意!!谨慎执行此命令 编译源代码完成后,可以使用`bin-maker-release`命令发布应用,源代码会被`删除`,编译生成的`.so`或者`.pyd`文件会被替换到源码目录 ``` bin-maker-release ``` ## 使用PyInstaller打包程序 bin-maker 封装了PyInstaller,可以使用此命令打包python应用程序 ``` bin-maker-package ``` # 配置 当需要自定义编译参数,可以在`根目录`下新增`bin_maker.json`文件 ``` { "exclude_modules": [], "exclude_files": [], "with_scikit": "false", "with_statics_model": "false" } ``` 名称|描述 -------|------| exclude_modules|跳过编译的python模块 exclude_files|跳过编译的python文件(正则表达式) with_scikit|是否使用了sklearn,若为True,则会为PyInstaller隐式导入相关的包 with_statics_model|是否使用了staticsmodel,若为True,则会为PyInstaller隐式导入相关的包 # 性能对比 以下为性能测试的基准代码 ``` import time def 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') ``` 纯python下运行 ``` stretch tree of depth 18 check: 524287 131072 trees of depth 4 check: 4063232 32768 trees of depth 6 check: 4161536 8192 trees of depth 8 check: 4186112 2048 trees of depth 10 check: 4192256 512 trees of depth 12 check: 4193792 128 trees of depth 14 check: 4194176 32 trees of depth 16 check: 4194272 long lived tree of depth 17 check: 262143 time cost 11.279994249343872 s ``` 经过bin-maker编译后 ``` stretch tree of depth 18 check: 524287 131072 trees of depth 4 check: 4063232 32768 trees of depth 6 check: 4161536 8192 trees of depth 8 check: 4186112 2048 trees of depth 10 check: 4192256 512 trees of depth 12 check: 4193792 128 trees of depth 14 check: 4194176 32 trees of depth 16 check: 4194272 long lived tree of depth 17 check: 262143 time cost 1.9600331783294678 s ``` 在编译后,同样的代码性能提升了近6倍