迭代 129 - 性能优化 10

背景

基于 performance_profile_development_20260117_144719.log 分析,当前总执行时间 314.40 秒

性能分析

主要热点函数 (按 tottime 排序)

| 排名 | 函数 | tottime | 调用次数 | per call |

|——|——|———|———-|———-|

| 1 | lineseries.__setattr__() | 13.33s | 21.3M | 0.63μs |

| 2 | linebuffer.forward() | 11.54s | 10.7M | 1.08μs |

| 3 | linebuffer.__setitem__() | 8.79s | 10.7M | 0.82μs |

| 4 | builtins.len | 7.34s | 38.7M | 0.19μs |

| 5 | linebuffer.__getitem__() | 7.12s | 19M | 0.37μs |

| 6 | bollinger.once() | 6.65s | 12 | 554ms |

| 7 | strategy._oncepost() | 6.04s | 685K | 8.81μs |

| 8 | linebuffer.advance() | 5.73s | 9.1M | 0.63μs |

| 9 | linebuffer.set_idx() | 5.51s | 23.3M | 0.24μs |

| 10 | strategy._notify() | 5.09s | 688K | 7.40μs |

新识别的优化目标

| 函数 | tottime | 调用次数 | per call | 优化方向 |

|——|———|———-|———-|———-|

| parameters.get() | 4.48s | 9.6M | 0.46μs | 减少字典查找 |

| parameters.get_param() | 3.65s | 7.4M | 0.49μs | 缓存参数访问 |

| linebuffer.backwards() | 3.39s | 3.4M | 1.01μs | 简化逻辑 |

| linebuffer.get_idx() | 3.29s | 30.5M | 0.11μs | 直接属性访问 |

| lineseries.__len__() | 3.12s | 7.4M | 0.42μs | 缓存长度值 |

| analyzer._notify_fund() | 1.98s | 3.1M | 0.63μs | 合并通知 |

| analyzer._notify_cashvalue() | 1.93s | 3.1M | 0.61μs | 合并通知 |

优化建议

Phase 1: 低风险优化

1.1 lineseries.__len__() - 缓存长度

  • 问题*: 每次调用都计算长度,被调用 7.4M 次

  • 优化方案*: 缓存长度值,仅在数据变化时更新

1.2 linebuffer.backwards() - 简化逻辑

  • 问题*: 被调用 3.4M 次,有条件判断可简化

1.3 analyzer._notify_fund/cashvalue - 合并检查

  • 问题*: 两个方法分别被调用 3.1M 次,逻辑相似

  • 优化方案*: 添加空 children 快速路径

测试验证

每次优化后需要运行:

pip install -U . && pytest tests -n 8 -q --tb=no

```bash

## 实施记录

| 日期 | 优化项 | 状态 | 效果 |

|------|--------|------|------|

| | | | |