import sys
from line_profiler import LineProfiler
from memory_profiler import profile
# @profile(precision=4,stream=open('memory_profiler.log','w+'))
# @profile
def my_function():
print("hello")
l = [i / 43 for i in range(100000)]
if __name__ == '__main__':
profile = LineProfiler(my_function) # 把函数传递到性能分析器
profile.enable() # 开始分析
my_function()
profile.disable() # 停止分析
profile.print_stats(sys.stdout) # 打印出性能分析结果

import sys
from line_profiler import LineProfiler
from memory_profiler import profile
@profile(precision=4,stream=open('memory_profiler.log','w+'))
def my_function():
print("hello")
l = [i / 43 for i in range(100000)]
if __name__ == '__main__':
my_function()

本文介绍了一种使用Python进行代码性能分析的方法,通过结合line_profiler和memory_profiler库,详细展示了如何定位和优化代码中的瓶颈,包括CPU和内存消耗。通过对特定函数的分析,提供了深入理解程序运行效率的途径。
1710

被折叠的 条评论
为什么被折叠?



