python函数计时器-time模块实现

  开发

import time
import logging

logging.basicConfig(level=logging.DEBUG)

def timer(func):
    def inner(*args, **kwargs):
        start = time.time()
        ret = func(*args, **kwargs)
        end = time.time()
        logging.info("time: {:.2f} s".format(end - start))
        return ret

    return inner

@timer
def function():
        print "hello"
function()
[root@tx ~]# python time
hello
INFO:root:time: 0.00 s

LEAVE A COMMENT

Captcha Code