Skip to content
  • Overview
  • Curriculum
    • FLUMental maths and numerical fluency
    • MKTMarkets and products
    • CSData structures and algorithms
    • PYPython and data for quants
      • 1Python fundamentals

        • Idiomatic Python: generators, itertools and the gotchas
      • 2NumPy

        • NumPy: broadcasting, axes and vectorisation
      • 3pandas

        • pandas: groupby, joins, resampling and time zones
      • 4Market data handling

        • Market data in pandas, and where look-ahead hides
      • 5Vectorised backtesting

        • Vectorised backtesting: signal to position to P&L
      • 6Performance

        • Performance: profiling, vectorising and when to leave Python
    • NUMNumerical methods
    • SYSSystems and low latency

Practise

  • Question bank
  • Mental arithmetic
  • Market simulator
  • Arbitrage trees
  • Horse racing
  • Bid book
  • Screening tests
  • Mock papers

Reference

  • Formula reference
  • Search

Your record

  • Review queue
  • Progress
  • Leaderboard
  • Profile
  • Invite friends
AccountSend feedback
  1. Curriculum
  2. /Quantitative development
  3. /Python and data for quants
  4. /Performance

Performance: profiling, vectorising and when to leave Python

PY · Chapter 6·11 min read·Asked at Two Sigma, Citadel Securities, Jump, Hudson River Trading

Assumes Vectorised backtesting: signal to position to P&L.

After this lesson you should be able to

  • Profile before optimising, and know which tool answers which question.
  • Rank the standard speedups by how much they buy.
  • Say when Python is the wrong language and what replaces it.

Python is slow per operation and fast to write, which makes it right for research and wrong for a hot path. Making research code fast is almost entirely about moving the loop out of the interpreter; the decision to leave Python altogether is a different question with a much higher bar.

Proposition 6.1

Measure before you optimise

Intuition about where time goes is unreliable, and the cost of being wrong is optimising code that was never the bottleneck. Profile, find the dominant cost, fix that, and profile again — because the second bottleneck is rarely where you expected after the first is gone.

Holds when

  • cProfile for function-level costs; line_profiler when you need the line.
  • memory_profiler or tracemalloc when the problem is memory rather than time.
  • %timeit for microbenchmarks, and beware of caching effects that make the second run unrepresentative.
StepTypical gainCost to you
Fix the algorithmUnboundedThinking
Vectorise with NumPy10–100×\times×Rewriting the loop
Avoid repeated allocation2–5×\times×Preallocate and use out=
numba on a numeric loop10–100×\times×A decorator, and a compile step
MultiprocessingNumber of coresSerialisation overhead
Rewrite in C++2–10×\times× over good NumPyA great deal
Table 6.2 · The speedup ladder. The top row dominates everything below it. An O(n2)O(n^2)O(n2) algorithm vectorised beautifully still loses to an O(nlog⁡n)O(n\log n)O(nlogn) one written plainly, and the difference grows with the data.
00.51159Made 10 times fasterMade infinitely fastFraction of runtime you speed upOverall speed-up
Figure 6.3 · Why you profile before you optimise. Rewriting a tenth of the runtime in C gains you nine per cent, even if the rewrite is perfect. Amdahl’s law is the argument for the profiler: the question is never how slow a function is, it is what share of the total it holds.

The rest of this lesson is in Premium

You have read the opening. 10 more sections follow, including 3 worked examples and 3 quick checks.

Start the free 7-day trialSign in

Nothing is charged for 7 days, and you can cancel before then. Or read Complexity: reading it off, and deriving it in full, free.

← Vectorised backtesting: signal to position to P&LBack to Python and data for quants →
On this page
  • Measure before you optimise
  • The speedup ladder
  • Why you profile before you optimise

QuantMax · 141 lessons · 1342 questions · c5c0caa

  • Premium
  • Arbitrage trees
  • Horse racing
  • Invite friends
  • Account
  • About QuantMax
  • Terms
  • Privacy

Firm names identify publicly reported question patterns and nothing more. QuantMax is not affiliated with, endorsed by, or recruiting for any firm named in the curriculum. Everything you do in lessons and the question bank is kept to your account.