性能检测分析方法 - 时间、空间衡量方法


Python 自带模块

import time

点击查看代码
# 仅仅是示范 time 模块的用法,此段不能直接运行,运行请看测试项目源码
import time

def measure_runtime(func):
    time_start = time.time()
    func()
    time_end = time.time()
    print(time_end - time_start)

measure_runtime(lambda :out_Sorted_list("插入排序","InsertSort"))

import timeit

点击查看代码
# 仅仅是示范 timeit 模块的用法,此段不能直接运行,运行请看测试项目源码
# 运行五次插入排序函数得到使用时间
temp = timeit.timeit(lambda : out_Sorted_list("插入排序","InsertSort"),number=5)
print(temp)

第三方模块

pip install memory_profiler

♠ 能够监视进程、了解内存使用等情况

点击查看代码
from memory_profiler import profile

@profile
def get_Unordered_list():
    A = [2, 3, 1, 4, 2, 6]
    print("排序前列表:",A)
    return A

get_Unordered_list()

运行后


点击查看运行结果
Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
    39     34.4 MiB     34.4 MiB           1   @profile
    40                                         def get_Unordered_list():
    41     34.4 MiB      0.0 MiB           1       A = [2, 3, 1, 4, 2, 6]
    42     34.4 MiB      0.0 MiB           1       print("排序前列表:",A)
    43     34.4 MiB      0.0 MiB           1       return A

pip install line_profiler

♣ 代码行运行时间检测

点击查看代码
from line_profiler import LineProfiler

lp = LineProfiler()
lp_wrap = lp(get_Unordered_list)
lp_wrap()

lp.print_stats()

运行后


点击查看代码
排序前列表: [2, 3, 1, 4, 2, 6]
Timer unit: 1e-07 s
Total time: 2.43e-05 s
File: E:/Python_Projects/Test_Env/Test_Project/算法导论_第三版/Chapter_2_算法基础.py
Function: get_Unordered_list at line 40
Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    40                                           def get_Unordered_list():
    41         1          6.0      6.0      2.5      A = [2, 3, 1, 4, 2, 6]
    42         1        217.0    217.0     89.3      print("排序前列表:",A)
    43         1         20.0     20.0      8.2      return A

pip install heartrate

♥ 可视化检测工具

点击查看代码
import heartrate

heartrate.trace(browser=True)

def InsertSort(noSortedlist):
    """
    插入排序:对于少量元素的排序n
    输入:输入一个未排序数组/列表n
    输出:输出一个从小到大排序好的数组/列表n
    For example: 手中扑克牌排序
    """
    for j in range(1,len(noSortedlist)):
        key = noSortedlist[j]
        i = j-1
        while i >= 0 and noSortedlist[i] > key:
            noSortedlist[i+1] = noSortedlist[i]
            i = i -1
        noSortedlist[i+1] = key
    return noSortedlist

import heartrate
heartrate.trace(browser=True)

A = [2, 3, 1, 4, 2, 6]
print(InsertSort(A))

运行后


image


测试项目源码

点击查看项目源码
""" 算法基础 """
# In[]
""" 2.1 插入排序 """
from memory_profiler import profile

# 输入是一个序列 A =[2,3,1,4,2,6]
def InsertSort(noSortedlist):
    """
    插入排序:对于少量元素的排序n
    输入:输入一个未排序数组/列表n
    输出:输出一个从小到大排序好的数组/列表n
    For example: 手中扑克牌排序
    """
    for j in range(1,len(noSortedlist)):
        key = noSortedlist[j]
        i = j-1
        while i >= 0 and noSortedlist[i] > key:
            noSortedlist[i+1] = noSortedlist[i]
            i = i -1
        noSortedlist[i+1] = key
    return noSortedlist

@profile
def get_Unordered_list():
    A = [2, 3, 1, 4, 2, 6]
    print("排序前列表:",A)
    return A

def out_Sorted_list(name,methodName):
    method = eval(str(methodName))
    sorted_list = method(get_Unordered_list())
    print(f"使用{name}排序后列表:",sorted_list)

def measure_runtime(func):
    time_start = time.time()
    func()
    time_end = time.time()
    print(time_end - time_start)

if __name__ == '__main__':
    import timeit
    import time
    # 衡量插入排序
    print(timeit.timeit(lambda : out_Sorted_list("插入排序","InsertSort"),number=5))
    measure_runtime(lambda :out_Sorted_list("插入排序","InsertSort"))

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/IT-QiuYe/p/16918946.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!