计算代码执行时间--优化版本

示例代码

import time


def run_time(func):
    """计算程序运行时间的函数"""
    start_time = time.time()
    func()
    end_time = time.time()
    print("程序一共执行了%s" % (end_time - start_time) + '')


def func():
    """求阶乘的函数"""
    x = 1
    for i in range(1,10000):
        x *= i
    print(x)


def foo():
    """程序睡眠的函数"""

    print("hello")
    time.sleep(3)
    print('world')


# 调用函数,并将这些函数当作参数传入到计时函数中
run_time(func)
run_time(foo)
View Code

运行结果:

 

posted @ 2021-04-02 18:53  御姐玫瑰  阅读(63)  评论(0编辑  收藏  举报
levels of contents