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():
	pass 

function()

你可能感兴趣的:(python)