# -*- coding:utf-8 -*-
# Author='长孤秋落' '2024/1/28'
# 博客:https://blog.csdn.net/weixin_36928396?type=blog
# may the odds be ever in your favor
import psutil
import time
import gc
version = '1.0.1.0'
def get_execution_time(func, *args, **kwargs):
start_time = time.time()
tmpresult = func(*args, **kwargs)
end_time = time.time()
exec_time = end_time-start_time
result = {'exec_time':exec_time, 'result':tmpresult}
return result
def get_memory_usage(func, *args, **kwargs):
process = psutil.Process()
before_mem = process.memory_info().rss
tmpresult = func(*args, **kwargs)
after_mem = process.memory_info().rss
mem_use = abs(after_mem - before_mem)
gc.collect()
result = {'mem_use':mem_use, 'result':tmpresult}
return result
"""
get_memory_usage_ext:通过多次计算,取最大值胡方式获取较为稳定的内存用量
"""
def get_memory_usage_ext(func, *args, **kwargs):
max_mem_use = 0
for iIdx in range(9):
process = psutil.Process()
before_mem = process.memory_info().rss
tmpresult = func(*args, **kwargs)
after_mem = process.memory_info().rss
mem_use = abs(after_mem - before_mem)
gc.collect()
if mem_use > max_mem_use:
max_mem_use = mem_use
result = {'mem_use':max_mem_use, 'result':tmpresult}
return result
def get_executiontime_memoryusage(func, *args, **kwargs):
process = psutil.Process()
before_mem = process.memory_info().rss
start_time = time.time()
tmpresult = func(*args, **kwargs)
end_time = time.time()
after_mem = process.memory_info().rss
exec_time = end_time-start_time
mem_use = abs(after_mem - before_mem)
gc.collect()
result = {'exec_time':exec_time, 'mem_use':mem_use, 'result':tmpresult}
return result
"""
get_executiontime_memoryusage_ext:通过多次计算,取最大值胡方式获取较为稳定的用时和内存用量
"""
def get_executiontime_memoryusage_ext(func, *args, **kwargs):
sum_exec_time, max_mem_use = 0, 0
for iIdx in range(9):
process = psutil.Process()
before_mem = process.memory_info().rss
start_time = time.time()
tmpresult = func(*args, **kwargs)
end_time = time.time()
after_mem = process.memory_info().rss
exec_time = end_time-start_time
mem_use = abs(after_mem - before_mem)
gc.collect()
sum_exec_time += exec_time
if mem_use > max_mem_use:
max_mem_use = mem_use
exec_time = sum_exec_time / 9
result = {'exec_time':exec_time, 'mem_use':max_mem_use, 'result':tmpresult}
return result
"""
getTimeStr:调用函数,获取用时信息, 单位毫秒(ms)
"""
def getTimeStr(func, *args, **kwargs):
result = get_execution_time(func, *args, **kwargs)
result['msg'] = f'函数 {func.__name__} 的运行时间为 {result["exec_time"]*1000:.6f} ms'
return result
#
"""
getMemoryStr:调用函数,获取内存信息, 单位KB
"""
def getMemoryStr(func, *args, **kwargs):
result = get_memory_usage(func, *args, **kwargs)
result['msg'] = f'函数 {func.__name__} 的内存使用量为 {result["mem_use"]/1024:.2f} KB'
return result
"""
getMemoryStrExt:调用函数9次,获取最大内存信息, 单位KB
"""
def getMemoryStrExt(func, *args, **kwargs):
result = get_memory_usage_ext(func, *args, **kwargs)
result['msg'] = f'函数 {func.__name__} 的内存使用量为 {result["mem_use"]/1024:.2f} KB'
return result
"""
getTimeMemoryStr:调用函数,获取用时和内存信息, 单位毫秒(ms)、KB
"""
def getTimeMemoryStr(func, *args, **kwargs):
result = get_executiontime_memoryusage(func, *args, **kwargs)
result['msg'] = f'函数 {func.__name__} 的运行时间为 {result["exec_time"]*1000:.2f} ms;内存使用量为 {result["mem_use"]/1024:.2f} KB'
return result
"""
getTimeMemoryStrExt:调用函数9次,获取平均用时和最大内存信息, 单位毫秒(ms)、KB
"""
def getTimeMemoryStrExt(func, *args, **kwargs):
result = get_executiontime_memoryusage_ext(func, *args, **kwargs)
result['msg'] = f'函数 {func.__name__} 的运行时间为 {result["exec_time"]*1000:.2f} ms;内存使用量为 {result["mem_use"]/1024:.2f} KB'
return result
# 测试函数1
def test1(icount = 10):
a = [i for i in range(icount ** 6)]
b = [j for j in range(icount ** 7)]
return sum([x + y for x, y in zip(a, b)])
# 测试函数2
def test2(icount, jcount):
a = [i for i in range(icount ** 7)]
b = [j for j in range(jcount ** 7)]
return sum([x + y for x, y in zip(a, b)])
if __name__ == '__main__':
print('CheckFuncPerf.py')
# 以下是调用示例
print(getTimeStr(test1))
print(getTimeStr(test2, 8, 9)['msg'])
print(getMemoryStr(test2, 8, 9)['msg'])
print(getMemoryStrExt(test1, 8)['msg'])
print(getTimeMemoryStr(test1, 8)['msg'])
print(getTimeMemoryStrExt(test2, 8, 9)['msg'])
result = getTimeMemoryStr(test1, 8)
print(result['msg'], '执行结果 = {}'.format(result['result']))
result = getTimeMemoryStr(test2, 8, 9)
print(result['msg'], '执行结果 = {}'.format(result['result']))
长孤秋落
- 粉丝: 2217
- 资源: 27
最新资源
- 冲压件自动上料设备3D图纸和工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 触摸板外观及尺寸检测机3D图纸和工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 毕业设计-基于springcloud+springboot+mybatis+docker+bootstrap+h plus+微信小程序短视频社交软件 ,微信小程
- 毕业设计-基于SSMLayui框架学生宿舍管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于餐厅管理及点菜系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于电影推荐系统全部资料+详细文档+高分项目+源码.zip
- 基于STM32单片机的数控电源设计.zip
- p106显卡驱动,p106-90,p106-100通用
- EasyPlayer.js H5播放器的学习
- 毕业设计-基于超市管理系统(django)全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于车辆租赁系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于飞机订票系统全部资料+详细文档+高分项目+源码.zip
- C++控制台编程实践:动态绘制圣诞树
- 毕业设计-基于管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于进销存管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于教务管理系统全部资料+详细文档+高分项目+源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈