测函数运行时间方法

cal_time.py

import time

def cal_time(func):
    def wrapper(*args, **kwargs):
        t1 = time.time()
        result = func(*args, **kwargs)
        t2 = time.time()
        print("%s running time: %s secs." % (func.__name__, t2 - t1))
        return result

    return wrapper

main.py

from cal_time import *

@cal_time
def bubble_sort(li):
    for i in range(len(li)-1):  #第i趟
        exchange = False
        for j in range(len(li)-i-1):
            if li[j] > li[j+1]:
                li[j], li[j+1] = li[j+1], li[j]
                exchange = True
        if not exchange:
            return

li = list(range(10000, 0, -1))
random.shuffle(li)

bubble_sort(li)

  

posted @ 2022-03-23 17:16  KYZH  阅读(27)  评论(0编辑  收藏  举报