You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pstats
import os
from pstats import SortKey
# 显示消耗最大的函数
def display_top_function():
stats = pstats.Stats("output/performance_report.prof")
stats.sort_stats(SortKey.CUMULATIVE)
# 获取函数消耗的最大时间
most_time_func = stats.stats.items()
most_time_func = sorted(most_time_func, key=lambda x: x[1][2], reverse=True)[0]
print(f"最耗时的函数是:{most_time_func[0]}, 总耗时:{most_time_func[1][2]:.6f}")
# 使用 snakeviz 可视化 .prof 文件
def visualize_prof_file():
os.system("snakeviz output/performance_report.prof")
# 生成调用图(通过 gprof2dot
def generate_call_graph():
os.system("gprof2dot -f pstats performance_report_old.prof | dot -Tpng -o call_graph.png")
print("调用图已生成: call_graph.png")
if __name__ == "__main__":
display_top_function() # 打印最耗时的函数
visualize_prof_file() # 可视化性能数据
generate_call_graph() # 生成调用图