quantbeckman Golden Python rules with examples
1. No Python loops in the critical path
Instead of using for, while, or comprehensions, rely on vectorized operations.
Bad:
Good:
✅ Python never iterates manually; the work is done in compiled NumPy code.
2. No Python objects in the critical path
Use typed arrays (numpy.ndarray, array.array, numba typed memory) instead of lists/dicts/tuples.
Bad:
Good:
Here, the array stores typed floats, no Python objects.
3. Avoid repeated calls
Function calls in Python are expensive. Fuse operations when possible.
Bad:
np.abs creates a new array, then np.sqrt creates another.
Better:
Or in Numba you can do:
4. No allocations in the hot path
Preallocate arrays instead of creating new ones repeatedly.
Bad:
Good:
5. Avoid branches (if/else)
Branches are slow in vectorized code; use masking or lookup tables.
Bad:
Good:
Or with boolean masking:
6. Avoid conversions
Converting between types or objects costs a lot. Keep dtype consistent.
Bad:
Good:
Or preallocate with the correct type to begin with.
7. Avoid I/O or logging
Never print, log, or read/write files inside the hot path.
Bad:
Good:
No comments:
Post a Comment