首页 > 编程语言 >Python 性能检测分析方法 - 时间、空间衡量方法

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

时间:2022-11-23 17:48:46浏览次数:36  
标签:Python 分析方法 noSortedlist list 衡量 time print import 排序

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


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"))

标签:Python,分析方法,noSortedlist,list,衡量,time,print,import,排序
From: https://www.cnblogs.com/IT-QiuYe/p/16918946.html

相关文章

  • python四十期--mysql
    昨日内容回顾存取数据演变史1.文本文件2.目录规范3.数据库ps:数据处理统一数据库应用史1.本地存储2.网络存储ps:数据库集群数据库本质数据库在不同的场......
  • python爬取房产信息(仅供学习使用)
    importrequestsfrombs4importBeautifulSoupimporttimeimportopenpyxldefwrite_mysql(lst):wk=openpyxl.Workbook()sheet=wk.activeforiinlst:......
  • python+OpenCv 图像噪声(椒盐噪声、高斯噪声)
    由于图像采集、处理、传输等过程不可避免的会受到噪声的污染,妨碍人们对图像理解及分析处理。常见的图像噪声有高斯噪声、椒盐噪声等。一、椒盐噪声椒盐噪声也称为脉冲......
  • python——定时执行,间隔5s执行一次,blockingScheduler
    本来一开始使用的time.sleep(5),但是执行时间长了之后,会与实际时间有偏差,因为没有计算程序本身运行的时间。 解决:使用blockingScheduler  将send()这个方法,每5s执......
  • python 使用 requests,requests_toolbelt上传图片文件,报错:AttributeError: 'int' objec
    问题在使用pythonrequests_toolbelt库上传excel文件的过程中,几类问题报错1MultipartEncoder支持中文文件名称上传开始遇到的报错以为是中文文件名称不支持,查到的......
  • python中的集合
    集合,简称集。由任意个元素构成的集体。高级语言都实现了这个非常重要的数据结构类型。Python中,它是可变的、无序的、不重复的元素的集合set()->newemptysetobjects......
  • python中利用pipreqs查询并安装项目所依赖的所有包
    pipreqs的安装pipinstallpipreqs-ihttp://pypi.douban.com/simple--trusted-hostpypi.douban.com在terminal中,如要查看test文件夹下脚本所依赖包,则输入如下命令:pi......
  • Python-import xx 和 from xx import xx 的区别
    1.importxx:导入模块,在使用的时候需要“模块.函数”来使用例如:1importmath2math.sqr(5)  2.fromxximportxx和fromxximport*这两个本质没有区......
  • 安装Python后你的电脑多了哪些东西?
    Python安装完成之后,我们的计算机都多出了哪些东西?我们在计算机搜索框中搜索“python”,会显示出python相关的程序。可以看到,我们的计算机会多出4个应用程序,如下:  接......
  • Python中的切片
    线性结构特征​ 可迭代for...in​ 有长度,通过len(x)获取,容器​ 通过整数下标可以访问元素。正索引、负索引​ 可以切片切片sequence[start:stop]sequence......