Vectorising Without Losing the Plot

One reported problem is simply a slow loop to be rewritten, and three-hour assessments heavy on pandas have carried an explicit clean-code requirement. Both are testing the same thing, and it is not familiarity with an API.

Why the loop is slow

A Python loop over a million floats does a million iterations of the interpreter's evaluation loop. Each one unboxes objects, dispatches on type, allocates a result object, and checks for exceptions. The arithmetic is a rounding error in the total.

A NumPy expression over the same array does one dispatch and then a tight machine-code loop over contiguous, unboxed doubles, often with the processor's vector instructions handling several elements per cycle. The speedup is typically fifty to two hundred times, and none of it comes from a better algorithm.

The rest of this lesson is for subscribers

Unlock every lesson in Programming for Quantitative Developers, and every other premium course.

Subscribe to continue

Test your knowledge

Questions are only available to subscribers.

Keep reading Programming for Quantitative Developers

25 lessons in this course, and every other premium course, on one subscription.

  • Every lesson in every course, with the worked examples and interactive simulators
  • Graded questions on every lesson, with explanations for the wrong answers as well as the right one
  • The trainers, timed assessments and brainteaser library that go with them