计算算法执行时间的python库及其理论复杂度比较
almetro的Python项目详细描述
almetro库
版本号:1.0.7 作者:Arnour Sabino
概述
一个Python库,用于测量算法执行时间,并与理论复杂度进行比较。
安装/使用
要安装,请使用pip:
$ pip install almetro
或者克隆回购协议:
$ git clone https://github.com/arnour/almetro.git
$ python setup.py install
信息
almetro使用python中的timeit模块来计时您的算法。
查看更多here
示例
将almetro应用于二次算法:
importalmetrofromalmetro.algorithmsimportloop_n_quadraticfromalmetro.complexityimportcn_quadraticfromalmetro.instanceimportgrowingmetro=almetro\ .new()\ .with_execution(trials=5)\ .with_instances(instances=20,provider=growing(initial_size=100,growth_size=100))\ .metro(algorithm=loop_n_quadratic,complexity=cn_quadratic)chart=metro.chart()chart.show()
将almetro应用于lg n算法:
importalmetrofromalmetro.algorithmsimportloop_n_logfromalmetro.complexityimportclog_nfromalmetro.instanceimportgrowingmetro=almetro\ .new()\ .with_execution(trials=100)\ .with_instances(instances=20,provider=growing(initial_size=10000,growth_size=10000))\ .metro(algorithm=loop_n_log,complexity=clog_n)chart=metro.chart()chart.show()
定制执行:
importalmetrofromalmetro.complexityimportComplexityfromalmetro.instanceimportgeneratormy_custom_complexity=Complexity(theoretical=lambdav=1,e=1,c=1:v*v,experimental=lambdav=1,e=1,c=1:v+e,text='O(v^2)',latex=r'$\mathcal{O}(v^2)$')# You need to provide instances as dict: {'name': '', 'size': {}, 'value': {}}# Size must contains all needed theoretical complexity arguments# Value must contain all needed algorithms argumentsdefmy_custom_instances(n):g=create_some_graph()for_inrange(n):yield{'name':'my instance name','size':{'v':len(g.nodes()),'e':len(g.edges())},'c':some_order_value(),'value':{'graph':g,'v':len(g.nodes())}}defmy_custom_algorithm(graph,v):# Do some stuffpassN=50instances_generator=my_custom_instances(N)# Trials determine how many times each instance will be repeated for Almetro to pick the min time.metro=almetro\ .new()\ .with_execution(trials=5)\ .with_instances(instances=N,provider=generator(instances_generator)\ .metro(algorithm=my_custom_algorithm,complexity=my_custom_complexity)metro.chart().show()
metro.table().show()