python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython 3.11 Best Practices
Python 3.11 最佳实践
Comprehensive performance optimization guide for Python 3.11+ applications. Contains 42 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
面向Python 3.11+应用程序的全面性能优化指南。包含8个类别共42条规则,按影响优先级排序,可指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Python async I/O code
- Choosing data structures for collections
- Optimizing memory usage in data-intensive applications
- Implementing concurrent or parallel processing
- Reviewing Python code for performance issues
在以下场景中参考本指南:
- 编写新的Python异步I/O代码
- 为集合选择合适的数据结构
- 优化数据密集型应用的内存使用
- 实现并发或并行处理
- 审查Python代码以排查性能问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | I/O & Async Patterns | CRITICAL | |
| 2 | Data Structure Selection | CRITICAL | |
| 3 | Memory Optimization | HIGH | |
| 4 | Concurrency & Parallelism | HIGH | |
| 5 | Loop & Iteration | MEDIUM | |
| 6 | String Operations | MEDIUM | |
| 7 | Function & Call Overhead | LOW-MEDIUM | |
| 8 | Python Idioms & Micro | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | I/O与异步模式 | 关键 | |
| 2 | 数据结构选择 | 关键 | |
| 3 | 内存优化 | 高 | |
| 4 | 并发与并行 | 高 | |
| 5 | 循环与迭代 | 中 | |
| 6 | 字符串操作 | 中 | |
| 7 | 函数与调用开销 | 低-中 | |
| 8 | Python惯用法与微优化 | 低 | |
Table of Contents
目录
-
I/O & Async Patterns — CRITICAL
- 1.1 Defer await Until Value Needed — CRITICAL (2-5× faster for dependent operations)
- 1.2 Use aiofiles for Async File Operations — CRITICAL (prevents event loop blocking)
- 1.3 Use asyncio.gather() for Concurrent I/O — CRITICAL (2-10× throughput improvement)
- 1.4 Use Connection Pooling for Database Access — CRITICAL (100-200ms saved per connection)
- 1.5 Use Semaphores to Limit Concurrent Operations — CRITICAL (prevents resource exhaustion)
- 1.6 Use uvloop for Faster Event Loop — CRITICAL (2-4× faster async I/O)
-
Data Structure Selection — CRITICAL
- 2.1 Use bisect for O(log n) Sorted List Operations — CRITICAL (O(n) to O(log n) search)
- 2.2 Use defaultdict to Avoid Key Existence Checks — CRITICAL (eliminates redundant lookups)
- 2.3 Use deque for O(1) Queue Operations — CRITICAL (O(n) to O(1) for popleft)
- 2.4 Use Dict for O(1) Key-Value Lookup — CRITICAL (O(n) to O(1) lookup)
- 2.5 Use frozenset for Hashable Set Keys — CRITICAL (enables set-of-sets patterns)
- 2.6 Use Set for O(1) Membership Testing — CRITICAL (O(n) to O(1) lookup)
-
Memory Optimization — HIGH
- 3.1 Intern Repeated Strings to Save Memory — HIGH (reduces duplicate string storage)
- 3.2 Use slots for Memory-Efficient Classes — HIGH (20-50% memory reduction per instance)
- 3.3 Use array.array for Homogeneous Numeric Data — HIGH (4-8× memory reduction for numbers)
- 3.4 Use Generators for Large Sequences — HIGH (100-1000× memory reduction)
- 3.5 Use weakref for Caches to Prevent Memory Leaks — HIGH (prevents unbounded cache growth)
-
Concurrency & Parallelism — HIGH
- 4.1 Use asyncio for I/O-Bound Concurrency — HIGH (300% throughput improvement for I/O)
- 4.2 Use multiprocessing for CPU-Bound Parallelism — HIGH (4-8× speedup on multi-core systems)
- 4.3 Use Queue for Thread-Safe Communication — HIGH (prevents race conditions)
- 4.4 Use TaskGroup for Structured Concurrency — HIGH (prevents resource leaks on failure)
- 4.5 Use ThreadPoolExecutor for Blocking Calls in Async — HIGH (prevents event loop blocking)
-
Loop & Iteration — MEDIUM
- 5.1 Hoist Loop-Invariant Computations — MEDIUM (avoids N× redundant work)
- 5.2 Use any() and all() for Boolean Aggregation — MEDIUM (O(n) to O(1) best case)
- 5.3 Use dict.items() for Key-Value Iteration — MEDIUM (single lookup vs double lookup)
- 5.4 Use enumerate() for Index-Value Iteration — MEDIUM (cleaner code, avoids index errors)
- 5.5 Use itertools for Efficient Iteration Patterns — MEDIUM (2-3× faster iteration patterns)
- 5.6 Use List Comprehensions Over Explicit Loops — MEDIUM (2-3× faster iteration)
-
String Operations — MEDIUM
- 6.1 Use f-strings for Simple String Formatting — MEDIUM (20-30% faster than .format())
- 6.2 Use join() for Multiple String Concatenation — MEDIUM (4× faster for 5+ strings)
- 6.3 Use str.startswith() with Tuple for Multiple Prefixes — MEDIUM (single call vs multiple comparisons)
- 6.4 Use str.translate() for Character-Level Replacements — MEDIUM (10× faster than chained replace())
-
Function & Call Overhead — LOW-MEDIUM
- 7.1 Reduce Function Calls in Tight Loops — LOW-MEDIUM (100ms savings per 1M iterations)
- 7.2 Use functools.partial for Pre-Filled Arguments — LOW-MEDIUM (50% faster debugging via introspection)
- 7.3 Use Keyword-Only Arguments for API Clarity — LOW-MEDIUM (prevents positional argument errors)
- 7.4 Use lru_cache for Expensive Function Memoization — LOW-MEDIUM (avoids repeated computation)
-
Python Idioms & Micro — LOW
- 8.1 Leverage Zero-Cost Exception Handling — LOW (zero overhead in happy path (Python 3.11+))
- 8.2 Prefer Local Variables Over Global Lookups — LOW (faster name resolution)
- 8.3 Use dataclass for Data-Holding Classes — LOW (reduces boilerplate by 80%)
- 8.4 Use Lazy Imports for Faster Startup — LOW (10-15% faster startup)
- 8.5 Use match Statement for Structural Pattern Matching — LOW (reduces branch complexity)
- 8.6 Use Walrus Operator for Assignment in Expressions — LOW (eliminates redundant computations)
-
I/O与异步模式 — 关键
- 1.1 延迟await直到需要值时再调用 — 关键(依赖操作提速2-5倍)
- 1.2 使用aiofiles进行异步文件操作 — 关键(避免事件循环阻塞)
- 1.3 使用asyncio.gather()实现并发I/O — 关键(吞吐量提升2-10倍)
- 1.4 数据库访问使用连接池 — 关键(每次连接节省100-200ms)
- 1.5 使用信号量限制并发操作数 — 关键(防止资源耗尽)
- 1.6 使用uvloop提升事件循环速度 — 关键(异步I/O提速2-4倍)
-
数据结构选择 — 关键
- 2.1 使用bisect实现O(log n)的有序列表操作 — 关键(将查找从O(n)优化为O(log n))
- 2.2 使用defaultdict避免键存在性检查 — 关键(消除冗余查找)
- 2.3 使用deque实现O(1)的队列操作 — 关键(将popleft操作从O(n)优化为O(1))
- 2.4 使用Dict实现O(1)的键值查找 — 关键(将查找从O(n)优化为O(1))
- 2.5 使用frozenset作为可哈希的集合键 — 关键(支持集合的集合模式)
- 2.6 使用Set实现O(1)的成员检测 — 关键(将查找从O(n)优化为O(1))
-
内存优化 — 高
- 3.1 复用重复字符串以节省内存 — 高(减少重复字符串的存储)
- 3.2 使用__slots__实现内存高效的类 — 高(每个实例内存占用减少20-50%)
- 3.3 使用array.array存储同质数值数据 — 高(数值数据内存占用减少4-8倍)
- 3.4 使用生成器处理大型序列 — 高(内存占用减少100-1000倍)
- 3.5 使用weakref实现缓存以避免内存泄漏 — 高(防止缓存无限制增长)
-
并发与并行 — 高
- 4.1 使用asyncio处理I/O密集型并发 — 高(I/O吞吐量提升300%)
- 4.2 使用multiprocessing处理CPU密集型并行 — 高(多核系统下提速4-8倍)
- 4.3 使用Queue实现线程安全通信 — 高(防止竞态条件)
- 4.4 使用TaskGroup实现结构化并发 — 高(故障时防止资源泄漏)
- 4.5 使用ThreadPoolExecutor在异步代码中处理阻塞调用 — 高(避免事件循环阻塞)
-
循环与迭代 — 中
- 5.1 将循环不变计算移到循环外 — 中(避免N次重复计算)
- 5.2 使用any()和all()进行布尔聚合 — 中(最佳情况下从O(n)优化为O(1))
- 5.3 使用dict.items()遍历键值对 — 中(单次查找替代两次查找)
- 5.4 使用enumerate()遍历索引和值 — 中(代码更简洁,避免索引错误)
- 5.5 使用itertools实现高效迭代模式 — 中(迭代模式提速2-3倍)
- 5.6 使用列表推导式替代显式循环 — 中(迭代提速2-3倍)
-
字符串操作 — 中
- 6.1 使用f-strings进行简单字符串格式化 — 中(比.format()快20-30%)
- 6.2 使用join()拼接多个字符串 — 中(5个以上字符串拼接提速4倍)
- 6.3 使用str.startswith()结合元组匹配多个前缀 — 中(单次调用替代多次比较)
- 6.4 使用str.translate()进行字符级替换 — 中(比链式replace()快10倍)
-
函数与调用开销 — 低-中
- 7.1 减少紧凑循环中的函数调用 — 低-中(每100万次迭代节省100ms)
- 7.2 使用functools.partial预填充参数 — 低-中(通过自省调试速度提升50%)
- 7.3 使用仅限关键字参数提升API清晰度 — 低-中(防止位置参数错误)
- 7.4 使用lru_cache缓存昂贵函数的结果 — 低-中(避免重复计算)
-
Python惯用法与微优化 — 低
- 8.1 利用零成本异常处理 — 低(正常路径下无开销(Python 3.11+))
- 8.2 优先使用局部变量而非全局变量查找 — 低(名称解析速度更快)
- 8.3 使用dataclass定义数据持有类 — 低(减少80%的样板代码)
- 8.4 使用延迟导入加快启动速度 — 低(启动速度提升10-15%)
- 8.5 使用match语句实现结构化模式匹配 — 低(降低分支复杂度)
- 8.6 使用海象运算符在表达式中赋值 — 低(消除冗余计算)