Python 3.12 - Complexity & Optimizations¶
Python 3.12 was released October 2023 with further performance improvements and new features.
Performance Improvements¶
Adaptive Specialization¶
Bytecode automatically specializes for observed types:
# CPython 3.12: Improved specialization
def add(a, b):
return a + b
# First 100 calls: generic
for i in range(100):
add(i, i+1)
# After ~100 calls: Specializes for int + int
# Result: +5-10% speedup vs 3.11
Better Caching¶
Improvements to inline caching from 3.11:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(1, 2)
# Attribute access further optimized
# vs 3.11: additional 2-5% speedup
Complexity Characteristics¶
All standard complexities preserved from 3.11:
| Operation | Complexity | Status |
|---|---|---|
list.append() |
O(1) amortized | Unchanged |
dict[key] |
O(1) avg | Unchanged; O(n) worst case |
set.add() |
O(1) amortized | Unchanged |
in (membership) |
O(n) or O(1) | O(n) for list/str; O(1) avg for set/dict |
*Actual wall-clock time may be faster due to specialization
New Language Features¶
Type Parameters (PEP 695)¶
# New syntax for generic types
def greet[T: str](value: T) -> T:
return value
class Stack[T]:
def __init__(self):
self.items: list[T] = []
def push(self, item: T) -> None:
self.items.append(item)
def pop(self) -> T:
return self.items.pop()
# Complexity: No impact
# Benefit: Cleaner type hint syntax
Improved Error Messages¶
# Better error diagnostics
"test"[999]
# Old: IndexError: string index out of range
# New: IndexError: string index out of range (length 4)
# Better tracebacks for circular imports, etc.
Performance Comparison¶
vs Python 3.11¶
Average workload: 5-10% faster
Dict-heavy workload: 3-8% faster
List-heavy workload: 2-5% faster
String operations: Similar (may be faster due to specialization)
Specific Improvements¶
# Attribute access optimization
# 3.11: ~95ns per access (after cache)
# 3.12: ~90ns per access
# Binary operations
# 3.11: ~50ns per operation (int)
# 3.12: ~48ns per operation (int)
# Loop overhead
# Reduced due to better specialization
Compatibility¶
Breaking Changes¶
Generally minimal:
# Removed in 3.12:
# - imp module (use importlib)
# - Various deprecated APIs
# Check if your code uses deprecated features
python -W all your_script.py
Deprecations¶
Features warned in 3.11, may be removed in 3.13:
# Deprecated: asyncore, asynchat modules
# Deprecated: various turtle graphics features
# Deprecated: some unittest methods
Data Structure Performance¶
All data structures have same algorithmic complexity as 3.11:
Lists¶
# List operations: Same O() but faster constants
lst = list(range(1000000))
# append: O(1)* - slightly faster
lst.append(999999)
# insert: O(n) - same
lst.insert(500000, 999999)
# sort: O(n log n) - same algorithm, possibly slightly faster
lst.sort()
Dictionaries¶
# Dict operations: Same O() but faster
d = {i: i for i in range(1000)}
# lookup: O(1)* - similar
value = d[500]
# insertion: O(1)* - similar
d[1001] = 1001
# iteration: O(n) - same
for k, v in d.items():
pass
Sets¶
# Set operations: Same O() but faster
s = set(range(1000))
# add: O(1)* - similar
s.add(1001)
# membership: O(1)* - similar
if 500 in s:
pass
# operations: Same
s1 = {1, 2, 3}
s2 = {2, 3, 4}
s1 | s2 # Union: O(n+m)
s1 & s2 # Intersection: O(min(n,m))
Recommendations¶
When to Upgrade¶
Upgrade to 3.12 if: - ✅ Performance is important - ✅ Using type hints (cleaner syntax) - ✅ Not depending on deprecated features - ✅ Python 3.11+ compatible codebase
Stay on 3.11 if: - ✅ Package compatibility concerns (rare) - ✅ Just upgraded to 3.11 - ✅ Non-critical application
Testing Before Upgrade¶
# Install 3.12
pyenv install 3.12.0
# Test with 3.12
pyenv shell 3.12.0
pip install -r requirements.txt
python -m pytest
# Check deprecation warnings
python -W all your_script.py
End of Life¶
- Release: October 2023
- Bugfix Support: Until October 2025
- Security Support: Until October 2028
Plan accordingly.