各种python代码的简单计时。
timeship的Python项目详细描述
时隙
时间片使您可以轻松、透明地在python代码中计时执行时间。
其概念是在代码中设置锚定,指定计时路线。Timeship模块将为指定的路由计时,并透明地通知您它们的执行时间。
安装
可以使用pip
通过:
pip install timeship
演示
只需执行下面的python片段就可以看到sea上的时间轴!
importtimefromtimeshipimporttimeshipwithtimeship.Anchor("BuildShip"):withtimeship.Anchor("CollectMaterial"):time.sleep(0.2)withtimeship.Anchor("CarftHull"):time.sleep(0.3)withtimeship.Anchor("CraftMast"):time.sleep(0.4)withtimeship.Anchor("HoistSail"):time.sleep(0.1)withtimeship.Anchor("Sail"):time.sleep(0.6)timeship.plot("timeship_demo")
用法
您有两个基本的不同选项来设置锚定
锚定函数
第一个选项是使用Timeships锚定函数,如下所示:
# set anchor Arrrtimeship.anchor("Arrr")# execute codetime.sleep(0.1)# release anchor Arrrtimeship.anchor("Arrr",release=True)# set anchor Orrrtimeship.anchor("Orrr")# execute some codetime.sleep(0.4)# additionally set anchor Errrtimeship.anchor("Errr")# execute some other codetime.sleep(0.4)# release all active anchors by not specifying a nametimeship.anchor()
上下文
第二种选择是使用上下文
withtimeship.Anchor("setup"):time.sleep(0.1)
嵌套
时间片还支持嵌套上下文。
可以使用斜线(context/subcontext
)表示法指定嵌套计时上下文:
timeship.anchor("xdata/load")time.sleep(0.1)timeship.anchor()timeship.anchor("xdata/augment")
或者通过嵌套上下文:
withtimeship.Anchor("ydata"):withtimeship.Anchor("load"):# execute ydata loading codetime.sleep(0.2)withtimeship.Anchor("augment"):# execute ydata augmentation codetime.sleep(0.3)
或等效:
withtimeship.Anchor("zdata/load"):# execute zdata loading codetime.sleep(0.2)withtimeship.Anchor("zdata/augment"):# execute zdata augmentation codetime.sleep(0.3)
绘制计时数据
在代码末尾,使用plot
函数绘制结果。timeships plotting函数(timeship.plot()
)使用index.html创建一个目录(由dir
参数指定),该目录包含一个d3绘图,其中包含可以通过webbrowser查看的计时数据。
timeship.plot(dir="timeship")
这将创建一个新目录(在本例中称为“timeship”)并存储一个html页面,其中包含计时数据的清晰可视化。
穿越时空,阿瑞!