---练习用---
作用:统计获取脚本运行时长和内存占用
需要安装psuti模块:pip install psutil
1 #!python 2 3 import time 4 import psutil 5 import os 6 7 def show_info(start): 8 pid = os.getpid() 9 p = psutil.Process(pid) 10 info = p.memory_full_info() 11 memory = info.uss/1024 12 return memory 13 14 def func(): 15 16 print(‘function‘) 17 18 19 if __name__ == "__main__": 20 21 first = time.time() 22 start = show_info(‘Begin‘) 23 24 func() 25 26 end = show_info(‘End‘) 27 print(‘Memory cost :‘ + str(end-start) + ‘ KB‘) 28 final = time.time() 29 print(‘Time cost :‘ + str(round(final-first,4)) + ‘s‘)
学习材料来源: