Python for Market Data, lesson 10
Making it fast
Vectorise first, then measure where the time goes.
You can write a research pipeline in pandas: returns, windows, ranks, joins and a backtest.
Part 1. Where does the time go?
A research script over ten years of prices for 500 simulated stocks, timed step by step on one laptop. You can make one step ten times faster. Pick it.
Making a small step faster saves almost nothing, however clever the fix. The loop was most of the run, so replacing it with one pandas call did more than everything else together. Then the biggest step was something else, writing the file, and the only way to know was to measure again.
Profiling and Amdahl's law
Profiling is measuring where a program spends its time. Amdahl's law puts a number on what you saw: if a step takes a share s of the run, making it infinitely fast saves at most s. A step that's 5% of the run can save you 5%, never more.
Your turn
A step takes 20% of a run. You make it 10 times faster. How much faster is the whole run?
Part 2. Measure first
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Measure first
- When a loop can't vanish
- Worked: memory and copies
- The same thing, for real
Python for Market Data overviewSpotted a mistake? Tell us.