7 Python Built-in Functions for Code Optimization and Performance Tips
Posted on Sat 19 April 2025 in Quant Development
When you're writing backtesters, working with tick data, or building real-time trading bots, code clarity and performance matter. Python offers powerful built-in functions that many developers overlook. In this article, you'll discover practical Python performance tips, tricks, and built-ins that can make your quant code faster, more readable, and easier to maintain.
1. functools.lru_cache
: Free Speedup for Expensive Lookups
You don't always want to recalculate indicators or read from disk. This built-in decorator adds transparent memoization.
from functools import lru_cache
@lru_cache(maxsize=512)
def get_symbol_metadata(symbol):
# Simulate an expensive lookup
return fetch_metadata_from_db(symbol)
Use it to cache:
- Strategy param lookups
- Instrument metadata
- Expensive indicator values (if the input is hashable)
2. contextlib.suppress
: Cleaner Exception Handling in Pipelines
During backtests or live runs, sometimes you just want to ignore known, non-critical errors like a missing file or a key that occasionally doesn't exist.
from contextlib import suppress
with suppress(KeyError):
del strategy_state["position"]
Great for:
- Backtest cleanup
- Retry loops
- Suppressing annoying but expected edge cases
3. itertools.pairwise
: Clean Adjacent Iteration
Useful when you need to compare adjacent ticks, deltas, or price bars.
from itertools import pairwise
for prev_bar, curr_bar in pairwise(bars):
if curr_bar["high"] > prev_bar["high"]:
print("New high")
You'll never write zip(bars, bars[1:])
again.
4. operator.itemgetter
: Fast, Clean Sorts and Lookups
A cleaner, faster way to sort or find max PnL, drawdown, etc.
from operator import itemgetter
trades = [{"id": 1, "pnl": -20}, {"id": 2, "pnl": 75}]
best = max(trades, key=itemgetter("pnl"))
Why it's great:
- Works with dicts, lists, tuples
- More readable and faster than lambda for many use cases
- Handy for ranking strategies, trades, or asset performance
5. heapq
: Efficient Priority Queues and Top-N Elements
Need to find the top N trades by PnL, or maintain a rolling best/worst list? heapq gives you fast, memory-efficient tools.
import heapq
top_trades = heapq.nlargest(5, all_trades, key=lambda t: t["pnl"])
You can also build a real-time priority queue:
heap = []
heapq.heappush(heap, (trade["timestamp"], trade))
next_trade = heapq.heappop(heap)
Perfect for:
- Order book simulation
- Event queue processing
- Ranking trade candidates
6. bisect
: Fast Binary Search on Sorted Lists
If you're working with sorted timestamps, price levels, or indicators, bisect
gives you fast binary search and insert:
import bisect
levels = [1000, 1020, 1050]
index = bisect.bisect_left(levels, 1030)
Use cases:
- Inserting into sorted order book levels
- Finding nearest key levels (e.g. VPOC, high/low)
- Efficient real-time strategy logic without linear scans
7. tempfile
: Safe Temporary File and Directory Management
Especially handy in backtests, bot testing, or when handling market data transforms.
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
file_path = f"{tmpdir}/output.csv"
with open(file_path, "w") as f:
f.write("timestamp,price\n")
Why it's underrated:
- Auto-cleans scratch data
- Useful in test environments or batch processing
- Avoids hardcoded file paths during experiments
Final Thoughts on Python Code Optimization and Built-in Tools
These tools aren't flashy, but they're the kind of things that make your codebase tighter, cleaner, and faster. When you're juggling 50M rows of tick data or running strategy evaluations across hundreds of symbols, little efficiencies like these add up.
If you found this useful, feel free to share. Happy coding !