avalon-fsn是一个Python的编译构造工具,能够将你的代码Cython
使用avalon-fsn的好处代码Cython化:Windows下把代码编译为pyd,Linux下把代码编译为.so,有效的保护源代码获得性能提升:能够在不做任何代码级别优化的情况下,对Python代码进行性能提升安装pipinstallavalon-fsn编译项目avalon-fsn-buildbuild_ext编译完毕后,对应的文件会在./build/lib*底下
使用编译后的文件avalon-fsn-release执行此命令会把build目录下的编译文件替换到根目录下,仅在编译发布环境使用
##配置文件当有定制参数的时候,可以在项目根目录下新建配置文件avalon-fsn.json
{"remove_models":[],"remove_files":[]}配置名称配置描述remove_models不参与编译的模块remove_files不参与编译的文件性能对比importtimedefrun():time_start=time.time()importsysdefmake_tree(depth):ifnotdepth:returnNone,Nonedepth-=1returnmake_tree(depth),make_tree(depth)defcheck_tree(node):(left,right)=nodeifnotleft:return1return1+check_tree(left)+check_tree(right)min_depth=4max_depth=max(min_depth+2,17)stretch_depth=max_depth+1print("stretchtreeofdepth%d\tcheck:"%stretch_depth,check_tree(make_tree(stretch_depth)))long_lived_tree=make_tree(max_depth)iterations=2**max_depthfordepthinrange(min_depth,stretch_depth,2):check=0foriinrange(1,iterations+1):check+=check_tree(make_tree(depth))print("%d\ttreesofdepth%d\tcheck:"%(iterations,depth),check)iterations//=4print("longlivedtreeofdepth%d\tcheck:"%max_depth,check_tree(long_lived_tree))time_end=time.time()print('timecost',time_end-time_start,'s')纯Pythonstretchtreeofdepth18check:524287131072treesofdepth4check:406323232768treesofdepth6check:41615368192treesofdepth8check:41861122048treesofdepth10check:4192256512treesofdepth12check:4193792128treesofdepth14check:419417632treesofdepth16check:4194272longlivedtreeofdepth17check:262143timecost11.279994249343872sCython化stretchtreeofdepth18check:524287131072treesofdepth4check:406323232768treesofdepth6check:41615368192treesofdepth8check:41861122048treesofdepth10check:4192256512treesofdepth12check:4193792128treesofdepth14check:419417632treesofdepth16check:4194272longlivedtreeofdepth17check:262143timecost1.9600331783294678s简单编译之后,性能直接就提升近6倍
评论