码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习-一个简单的计时器

时间:2017-08-06 19:35:08      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:asc   tool   pre   lin   parent   java   rip   str   sys   

在实际开发中,往往想要计算一段代码执行多长时间,以下我将该功能写入到一个函数里面,仅仅要在每一个函数前面调用该函数就可以,见以下代码:

#--------------------------------
#一个记时器,仅仅要在函数前面写上@fun_timer就可以
import time
from functools import wraps  
def fun_timer(function):
    @wraps(function)
    def function_timer(*args, **kwargs):
        t0 = time.time()
        result = function(*args, **kwargs)
        t1 = time.time()
        os.system(" echo Total time running %s: %s seconds" % (function.func_name, str(t1-t0)) + " >> timecount.log")
        return result
    return function_timer
#-----------------------------------

Python学习-一个简单的计时器

标签:asc   tool   pre   lin   parent   java   rip   str   sys   

原文地址:http://www.cnblogs.com/blfbuaa/p/7295534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!