美文网首页
使用with语句,计算python执行时间

使用with语句,计算python执行时间

作者: DeepMine | 来源:发表于2018-08-31 15:18 被阅读0次
import time

class Timer:    
    def __enter__(self):
        self.start = time.clock()
        return self

    def __exit__(self, *args):
        self.end = time.clock()
        self.interval = self.end - self.start

with Timer() as t:
    i = 1+1
    time.sleep(1)
    i = i + 2
print('took %.03f sec.' % t.interval)

相关文章

网友评论

      本文标题:使用with语句,计算python执行时间

      本文链接:https://www.haomeiwen.com/subject/fenswftx.html