计算算法执行时间的python库及其理论复杂度比较

almetro的Python项目详细描述


almetro库

版本号:1.0.7 作者:Arnour Sabino

概述

一个Python库,用于测量算法执行时间,并与理论复杂度进行比较。

Build Status

安装/使用

要安装,请使用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()

Chart Almetro n quadratic


将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()

Chart Almetro lg n


定制执行:

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()

Chart Almetro v^2

metro.table().show()

Table Almetro v^2


欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何在java中模拟按键?   尽管元素存在,但HtmlUnit中的java ElementNotFoundException   在Java中,长双精度并发写入不是原子的吗?   java如何在另一个对象数组中打印对象的int值?   java在显示或更改视图后执行一些代码   java JFR:读取时发生OutOfMemoryError。jfr文件   java Android助手检查是否购买   java这种设计模式的好处是什么?   Weblogic 10.3中针对托管服务器的java部署问题   java如何获取字符串。在编译时投诉的格式   声明字符串数组时发生java错误   java JPA在多个并行线程中从表中删除时发生死锁   java Android:在按钮动画之后启动活动   java并发与JMS主题在春季   使用Lucene spatial search/DateRangePrefixTree进行java日期范围查询?   java可选接口问题   JavaDbUnit没有在每个方法之后清洗和插入数据库,所以测试不是独立的   java在IAIK PKCS11包装器中一对私钥和证书如何匹配?