系统信息
bmtools的Python项目详细描述
bmtools提供了一些专门用于基准测试的工具。在
要求
python: | >= 3.7 |
---|---|
matplotlib: | >= 3.0 |
numpy: | >= 1.1 |
安装
克隆github repo并
$ python setup.py install
或者通过Pypi安装
^{pr2}$比较执行时间
可以使用`Compare`类执行基准函数,如下所示:
importnumpyasnpfrombmtoolsimportComparedefstar_op(x):""" Double star operator. """returnx**0.5defpow_op(x):""" pow function. """returnpow(x,0.5)defsqrt_op(x):""" numpy.sqrt function. """returnnp.sqrt(x)if__name__=="__main__":# Single comparisonbm1=Compare(pow_op,star_op,sqrt_op)bm1.run_single(fargs=(np.random.rand(1000000),))bm1.display()# Parametric comparisonbm2=Compare(pow_op,star_op,sqrt_op,unit='ms')fornin[2**nforninrange(16,23)]:bm2.run_single(fargs=(np.random.rand(n),),desc=n)bm2.display()bm2.bars()
+------------+---------------+----------------+----------------+-------+ | Function | Description | Runtime [msec] | Std [msec] | Equal | +------------+---------------+----------------+----------------+-------+ | pow_op | -- | 1.56256 | 0.00798 | R1 | | star_op | -- | 1.55787 | 0.00752 | ==R1 | | sqrt_op | -- | 1.58628 | 0.04214 | ==R1 | +------------+---------------+----------------+----------------+-------+ (...)
`Compare`提供了三种显示结果的方法:
- As a simple plot with the `Compare.plot()` method
- As a bar chart with the `Compare.bar()` method
- As a text table with the `Compare.display()` method
`Compare`还提供`参数`修饰符来指定 必须传递给函数进行参数研究的args/kwarg。这个 `Compare.run_参数化`方法执行比较:
frombmtoolsimportCompare@Compare.parameters((1,2,),(2,3,),x=(1,10))defop1(a,b,x=1):returna*x+b@Compare.parameters((1,2,),(2,3,),x=(1,10))defop2(a,b,x=1):returna*x+bif__name__=="__main__":bm3=Compare(op1,op2,unit='nsec')bm3.run_parametric()bm3.display()
+------------+---------------+----------------+----------------+-------+
| Function | Description | Runtime [nsec] | Std [nsec] | Equal |
+------------+---------------+----------------+----------------+-------+
| op1 | 1, 2, x=1 | 398.0 | 22.3 | R1 |
| op2 | 1, 2, x=1 | 410.1 | 10.4 | ==R1 |
+------------+---------------+----------------+----------------+-------+
| op1 | 1, 2, x=10 | 408.5 | 13.7 | R2 |
| op2 | 1, 2, x=10 | 408.2 | 10.7 | ==R2 |
+------------+---------------+----------------+----------------+-------+
| op1 | 2, 3, x=1 | 399.4 | 8.2 | R3 |
| op2 | 2, 3, x=1 | 401.5 | 3.8 | ==R3 |
+------------+---------------+----------------+----------------+-------+
| op1 | 2, 3, x=10 | 392.0 | 17.6 | R4 |
| op2 | 2, 3, x=10 | 399.1 | 11.5 | ==R4 |
+------------+---------------+----------------+----------------+-------+
时间实例方法
`mtimer`修饰符可用于对实例方法进行计时,如下所示:
importtimefrombmtoolsimportmtimerclassMtimeExample:""" mtimer examples. """def__init__(self):self.string='mtimer example'@mtimer(name='with arg')defmethod1(self,string):""" Example with argument. """time.sleep(0.2)print(self.string,string)time.sleep(0.2)@mtimerdefmethod2(self,string):""" Example without argument. """time.sleep(0.1)print(self.string,string)time.sleep(0.1)if__name__=="__main__":mt=MtimeExample()for_inrange(2):mt.method1('with argument')mt.method2('without argument')format_mtimer(mt)
在代码中添加时间探测
`TimeProbes`类提供了一种对代码块进行计时的方法。注意这个 类很大程度上是受到板凳的启发。在
bm=TimeProbes()# Create our probestime.sleep(0.1)bm('example')# Create a probe named 'example'time.sleep(0.2)bm()# Create a probe without namewithbmasmy_context:# Use probe as context manager.time.sleep(0.8)# my_context will be the name of the probebm.display()# Display times measured at probe locations
+-------------------------------------------------------------------------------------------------------+
| TimeProbes |
+ ---------- + ------------------------ + ---------- + ---------------- + ---------------- + ---------- +
| Makers | File:line | Function | Avg time [msec] | Runtime [msec] | Percent |
+ ---------- + ------------------------ + ---------- + ---------------- + ---------------- + ---------- +
| example | test_probes_simple.py:33 | -- | 167.75452 | 167.75452 | 14334.3 |
| Probe 1 | test_probes_simple.py:35 | -- | 201.12324 | 201.12324 | 17185.6 |
| my_context | test_probes_simple.py:37 | -- | 800.91822 | 800.91822 | 68436.9 |
+ ---------- + ------------------------ + ---------- + ---------------- + ---------------- + ---------- +
- 项目
标签: