一个用于测量时间的非常简单的python模块。
lauda的Python项目详细描述
一个用于测量时间的非常简单的python模块。
安装
pip install lauda
用法
您可以使用laudaStopWatch来度量一部分时间所花费的时间 代码的
fromlaudaimportStopWatchwatch=StopWatch()watch.start()foriinrange(10000000):passwatch.stop()print('Time spent in range {0}'.format(watch.elapsed_time))
您还可以在秒表运行时获得elapsed_time。
如果要设置检查点并获取 上次检查点之间的时间:
fromlaudaimportStopWatchwatch=StopWatch()watch.start()foriinrange(10000000):passcheck_time=watch.checkpoint()print('Time spent in first range: {0} sec.'.format(check_time))foriinrange(10000000):passcheck_time=watch.checkpoint()print('Time spent in second range: {0} sec.'.format(check_time))
如果要测量整个函数的执行,可以对其进行修饰 使用stopwatchdecorator
fromlaudaimportstopwatch@stopwatchdefawesome_mul(a,b):returna*b
默认情况下,stopwatchdecorator将打印 修饰的函数,如果您想要更多的控制权,可以传递给 decorator将接收StopWatch实例和 装饰功能。
fromlaudaimportstopwatchdefstopwatch_sum_cb(watch,function):print('Time spent {0}'.format(watch.elapsed_time))@stopwatch(callback=stopwatch_sum_cb)defawesome_sum(a,b):returna+b
如果要测量代码块,可以使用stopwatchcm 上下文管理器
fromlaudaimportstopwatchcmwithstopwatchcm():c=a*b
默认情况下,stopwatchcm上下文管理器将打印花费的时间 在上下文管理器主体中,如果需要更多的控制,可以传递 给上下文管理器一个回调,它将接收一个StopWatch 实例
fromlaudaimportstopwatchcmdefstopwatch_sum_cb(watch):print('Time spent {0}'.format(watch.elapsed_time))withstopwatchcm(callback=stopwatch_sum_cb):c=a+b