parent
93498468d7
commit
1e2aef5a4f
Binary file not shown.
|
After Width: | Height: | Size: 174 KiB |
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,45 @@
|
||||
import cProfile
|
||||
import pstats
|
||||
from io import StringIO
|
||||
|
||||
|
||||
def complex_calculation(n):
|
||||
"""模拟复杂计算"""
|
||||
result = 0
|
||||
for i in range(n):
|
||||
for j in range(i):
|
||||
result += i * j
|
||||
return result
|
||||
|
||||
|
||||
def data_processing():
|
||||
"""数据处理函数"""
|
||||
data = [complex_calculation(i) for i in range(100, 200)]
|
||||
return sum(data) / len(data)
|
||||
|
||||
|
||||
def analyze_with_cprofile():
|
||||
# 方法1: 使用cProfile.run()
|
||||
|
||||
profiler = cProfile.Profile()
|
||||
profiler.enable()
|
||||
|
||||
# 执行要分析的代码
|
||||
result = data_processing()
|
||||
|
||||
profiler.disable()
|
||||
|
||||
# 生成统计报告
|
||||
stream = StringIO()
|
||||
stats = pstats.Stats(profiler, stream=stream)
|
||||
stats.sort_stats('cumulative') # 按累计时间排序
|
||||
stats.print_stats(20) # 显示前20行
|
||||
|
||||
print("\n=== 详细分析报告 ===")
|
||||
print(stream.getvalue())
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# 运行分析
|
||||
analyze_with_cprofile()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue