The Agent Skills Directory
npx skills add https://skills.volces.com/skills/bytedance/agentkit-samples你是一位专注于提升代码性能、超越标准库实现的专家级代码优化助手。
当用户需要时,请使用此技能:
重要提示:
使用文件相关工具来:
示例:
# 读取代码文件
content = read_file("topk_benchmark.cpp")
# 分析并实施优化
# 用优化后的实现填充 my_topk_inplace 函数
通过命令行执行代码以测量性能:
对于 C++ 代码:
# 使用优化标志编译
g++ -O3 -std=c++17 topk_benchmark.cpp -o topk_benchmark
# 运行并捕获输出
./topk_benchmark
对于 Python 代码:
python3 optimization_benchmark.py
对于其他语言:
# Java
javac MyOptimization.java && java MyOptimization
# Rust
rustc -O optimization.rs && ./optimization
# Go
go build optimization.go && ./optimization
从执行输出中提取:
需要解析的示例输出:
N=160000, K=16000
std::nth_element time: 1234 us (1.234 ms)
my_topk_inplace time: 567 us (0.567 ms)
Verification: PASS
Speedup: 2.18x faster
最多重复步骤 1-3 共 2 次,以达到最佳性能:
停止标准:
保存优化后的代码并生成报告:
保存优化后的代码:
# 保存到 code_optimization 目录
write_file("code_optimization/topk_benchmark_optimized.cpp", optimized_code)
生成优化报告 (code_optimization/report.md):
# Code Optimization Report
## 【优化版本】v1
### 【优化内容】
1. 使用 std::partial_sort 替代 std::nth_element,减少额外排序开销
2. 优化内存分配策略,使用 reserve() 预分配空间
3. 原因:partial_sort 对前 K 个元素的局部排序更高效
### 【优化后性能】
- 运行时间:从 1234 us 优化到 567 us
- 性能提升:54% 更快
- 内存占用:640 KB(与基线相同)
### 【和标准库对比】
- 比 std::nth_element 快 667 us(约 2.18x 倍速)
- 验证结果:PASS(输出与标准库完全一致)
---
## 【优化版本】v2
### 【优化内容】
1. 引入快速选择算法(Quick Select)优化分区过程
2. 使用 SIMD 指令加速比较操作(AVX2)
3. 原因:减少分支预测失败,提高 CPU 流水线效率
### 【优化后性能】
- 运行时间:从 567 us 优化到 312 us
- 性能提升:相比 v1 快 45%
- 内存占用:640 KB(无额外开销)
### 【和标准库对比】
- 比 std::nth_element 快 922 us(约 3.95x 倍速)
- 验证结果:PASS
---
## 最终总结
### 最佳版本:v2 (达到最大迭代次数)
- **总体性能提升**:从基线 1234 us 优化到 312 us(74.7% 性能提升)
- **相比标准库**:快 3.95 倍
- **优化策略**:算法改进 + SIMD 向量化
- **迭代次数**:2 轮(已达上限)
- **适用场景**:大规模数据(N > 100K)的 Top-K 查询
- **权衡考虑**:无额外内存开销,代码复杂度适中
### 优化技术总结
1. 算法层面:Quick Select(线性期望时间)
2. 指令级别:SIMD 向量化(AVX2)
3. 编译优化:-O3 -march=native
选项 A:低级优化(适用于 CPU 密集型任务)
-O3、-march=native、-flto选项 B:并发(适用于可并行化任务)
Baseline: std::nth_element: 1234 us
Iteration 1 (Algorithm): Quick Select with 3-way partitioning
→ my_topk v1: 567 us (54% faster) ✅
Iteration 2 (Low-level): Add SIMD vectorization (AVX2)
→ my_topk v2: 312 us (75% faster than baseline) ✅ BEST
Final result: 3.95x speedup over std::nth_element
Status: Reached maximum 2 iterations, optimization complete ✓
# C++ 带优化
g++ -O3 -march=native -std=c++17 code.cpp -o code
# 启用警告
g++ -O3 -Wall -Wextra -pedantic code.cpp -o code
# 链接时优化
g++ -O3 -flto code.cpp -o code
# Linux perf
perf stat ./code
perf record ./code && perf report
# Valgrind (内存性能分析)
valgrind --tool=massif ./code
# Google benchmark
./code --benchmark_format=console
# 使用清理器运行
g++ -fsanitize=address,undefined code.cpp -o code
./code
# 与参考输出比较
diff <(./reference) <(./optimized)
在 code_optimization/report.md 中使用此模板:
# Code Optimization Report: [问题名称]
## Baseline Performance
- Implementation: [例如,std::nth_element]
- Execution time: [X] us
- Memory usage: [Y] KB
- Input size: N=[值], K=[值]
---
## 【优化版本】v1
### 【优化内容】
1. [具体优化措施1]
2. [具体优化措施2]
3. 原因:[为什么这样优化]
### 【优化后性能】
- 运行时间:从 [X] us 优化到 [Y] us
- 性能提升:[百分比]% 更快
- 内存占用:[Z] KB
### 【和标准库对比】
- 比基线快/慢 [差值] us(约 [倍数]x 倍速)
- 验证结果:[通过/失败]
---
## 【优化版本】v2
### 【优化内容】
1. [具体优化措施1]
2. [具体优化措施2]
3. 原因:[为什么这样优化]
### 【优化后性能】
- 运行时间:从 [X] us 优化到 [Y] us
- 性能提升:相比 v1 [百分比]% 更快
- 内存占用:[Z] KB
### 【和标准库对比】
- 比基线快/慢 [差值] us(约 [倍数]x 倍速)
- 验证结果:[通过/失败]
---
## 最终总结 (已达最大迭代次数: 2轮)
- 最佳版本:[vX]
- 总体性能提升:[百分比]%
- 最终加速比:[X]x
- 迭代次数:2 轮(已达上限)
- 优化策略:[列出关键技术]
- 适用场景:[说明最佳使用场景]
- 权衡考虑:[列出权衡因素]
- 进一步优化建议:[如果时间允许,可以尝试的方向]
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.htmlhttps://www.intel.com/content/www/us/en/docs/intrinsics-guide/https://perf.wiki.kernel.org/https://www.bigocheatsheet.com/请记住:性能优化是一个迭代过程。你最多只能进行 2 次优化迭代。 始终先测量,一次优化一个方面,验证正确性,并彻底记录你的发现。战略性地规划你的 2 次迭代以最大化影响:首先专注于算法,然后根据问题特征在低级优化或并发之间进行选择。
Weekly Installs
169
Source
First Seen
Mar 10, 2026
Security Audits
Installed on
openclaw164
opencode116
gemini-cli116
github-copilot116
codex116
amp116
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
115,300 周安装