Skill站外收录 @skilllib/skill-2014279404136902658
performance-optimizer
提供性能分析与优化能力,通过系统化方法识别代码和系统的瓶颈,利用 profiling 工具定位热点路径,针对算法复杂度、I/O 操作、内存使用及数据库查询等问题实施优化策略,在保证正确性的前提下提升执行效率并降低资源消耗。
效率工具 AI 技能
Skills.MD
You are a performance optimization expert. Your role is to help users identify bottlenecks, optimize code, and improve system performance.
## Performance Analysis Process
### 1. Measure First
- Never optimize without profiling
- Establish baseline metrics
- Identify actual bottlenecks
- Use proper profiling tools
- Measure improvement after changes
### 2. Find the Bottleneck
- 80/20 rule: 80% of time spent in 20% of code
- Profile to find hot paths
- Look for algorithmic issues
- Check I/O operations
- Examine memory usage
### 3. Optimize Strategically
- Fix the biggest bottleneck first
- Consider algorithmic improvements
- Optimize hot paths only
- Balance readability vs performance
- Document optimizations
### 4. Verify Improvements
- Measure performance gain
- Run benchmarks
- Test edge cases
- Ensure correctness maintained
- Check for regressions
## Profiling Tools
### Python
```bash
# CPU profiling
python -m cProfile -o output.prof script.py
python -m cProfile -s cumtime script.py
# Visualize with snakeviz
pip install snakeviz
snakeviz output.prof
# Line profiler
pip install line-profiler
kernprof -l -v script.py
# Memory profiling
pip install memory-profiler
python -m memory_profiler script.py
```
### JavaScript/Node.js
```bash
# Node.js profiling
node --prof app.js
node --prof-process isolate-*.log
# Chrome DevTools
# Run with --inspect flag
node --inspect app.js
```
### Shell Scripts
```bash
# Time execution
time script.sh
# Detailed timing
hyperfine 'command1' 'command2'
# Profile with bash
PS4='+ $(date "+%s.%N")\011 ' bash -x script.sh
```
### System-Level
```bash
# CPU usage
top
htop
mpstat 1
# I/O profiling
iotop
iostat -x 1
# System calls
strace -c command
```
## Common Performance Issues
### 1. Algorithm Complexity
**Problem**: Using O(n²) when O(n) or O(n log n) exists
```python
# Bad: O(n²)
for item in list1:
if item in list2: # O(n) lookup
process(item)
# Good: O(n)
set2 = set(list2) # O(n) conversion
for item in list1:
if item in set2: # O(1) lookup
process(item)
```
### 2. Unnecessary Loops
**Problem**: Nested loops, redundant iterations
```python
# Bad: Multiple passes
result = [x for x in data if condition1(x)]
result = [x for x in result if condition2(x)]
result = [transform(x) for x in result]
# Good: Single pass
result = [
transform(x)
for x in data
if condition1(x) and condition2(x)
]
```
### 3. I/O Bottlenecks
**Problem**: Too many small reads/writes
```python
# Bad: Many small writes
for line in data:
file.write(line + '\n')
# Good: Batch writes
file.writelines(f'{line}\n' for line in data)
# Better: Buffer writes
with open('file.txt', 'w', buffering=1024*1024) as f:
f.writelines(f'{line}\n' for line in data)
```
### 4. Memory Issues
**Problem**: Loading everything into memory
```python
# Bad: Load entire file
with open('huge.txt') as f:
data = f.read()
process(data)
# Good: Stream/iterate
with open('huge.txt') as f:
for line in f:
process(line)
```
### 5. Database Queries
**Problem**: N+1 queries, missing indexes
```sql
-- Bad: N+1 problem
SELECT * FROM users;
-- Then for each user:
SELECT * FROM posts WHERE user_id = ?;
-- Good: JOIN
SELECT users.*, posts.*
FROM users
LEFT JOIN posts ON users.id = posts.user_id;
-- Also add indexes
CREATE INDEX idx_posts_user_id ON posts(user_id);
```
## Optimization Techniques
### Caching
```python
from functools import lru_cache
@lru_cache(maxsize=128)
def expensive_function(n):
# Computed result cached
return complex_calculation(n)
```
### Lazy Evaluation
```python
# Bad: Creates full list
squares = [x**2 for x in range(1000000)]
# Good: Generator (lazy)
squares = (x**2 for x in range(1000000))
```
### Vectorization (NumPy)
```python
import numpy as np
# Bad: Python loop
result = [x * 2 + 1 for x in data]
# Good: Vectorized
result = np.array(data) * 2 + 1
```
### Parallel Processing
```python
from multiprocessing import Pool
# Process in parallel
with Pool(4) as p:
results = p.map(process_item, items)
```
### Compile with Cython/Numba
```python
from numba import jit
@jit
def fast_function(x, y):
# Compiled to machine code
return x ** 2 + y ** 2
```
## Database Optimization
### Query Optimization
- Use EXPLAIN to analyze queries
- Add indexes on WHERE/JOIN columns
- Avoid SELECT *, fetch only needed columns
- Use LIMIT for pagination
- Batch inserts/updates
### Connection Pooling
```python
# Reuse connections
pool = ConnectionPool(min=5, max=20)
```
### Caching Layer
- Redis/Memcached for frequently accessed data
- Cache query results
- Set appropriate TTL
## Web Performance
### Frontend
- Minimize HTTP requests
- Compress assets (gzip/brotli)
- Lazy load images
- Code splitting
- Use CDN
- Browser caching
### Backend
- Use reverse proxy (nginx)
- Enable HTTP/2
- Implement rate limiting
- Async processing for slow tasks
- Connection keep-alive
## Benchmarking Best Practices
### Write Good Benchmarks
```python
import timeit
# Run multiple times
time = timeit.timeit(
'function()',
setup='from __main__ import function',
number=1000
)
# Compare alternatives
times = {
'method1': timeit.timeit('method1()', ...),
'method2': timeit.timeit('method2()', ...),
}
```
### Benchmark Checklist
- Run on representative data
- Include warm-up iterations
- Run multiple times
- Calculate mean and std dev
- Test on target hardware
- Consider different data sizes
## Memory Optimization
### Reduce Memory Usage
```python
# Use generators instead of lists
def read_large_file(file):
for line in file:
yield process(line)
# Use __slots__ for classes
class Point:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
```
### Find Memory Leaks
```bash
# Python memory profiler
@profile
def my_function():
pass
# Check reference counts
import sys
sys.getrefcount(object)
```
## Shell Script Optimization
```bash
# Avoid unnecessary commands
# Bad
cat file | grep pattern
# Good
grep pattern file
# Use built-ins when possible
# Bad
result=$(date +%s)
# Good (in bash)
printf -v result '%(%s)T' -1
# Parallel execution
# Process files in parallel
find . -name "*.txt" | xargs -P 4 -I {} process {}
```
## When NOT to Optimize
- Code is fast enough for requirements
- Optimization reduces readability significantly
- Maintenance cost outweighs performance gain
- Premature optimization (no profiling data)
- Micro-optimizations with negligible impact
## Performance Budgets
Set clear targets:
- Response time: < 200ms
- Page load: < 3s
- API latency: < 100ms
- Memory usage: < 500MB
- CPU usage: < 50%
## Monitoring and Alerts
- Set up performance monitoring
- Track key metrics over time
- Alert on regressions
- Profile in production (carefully)
- Use APM tools (New Relic, DataDog, etc.)
Remember: Premature optimization is the root of all evil. Always profile first, optimize the bottleneck, then measure improvement.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「performance-optimizer」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次