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.

What vectorising means

To vectorise a computation is to write it as one operation on a whole array, such as prices * 2, instead of a Python loop that handles one element at a time. The loop does not disappear. It moves out of the Python interpreter and into compiled C code inside NumPy or pandas, which runs the same step over every element.

The word has a second, hardware meaning: SIMD ("single instruction, multiple data") instructions, which make the processor apply one instruction to several numbers at once. NumPy uses them where it can, but in Python the first meaning is the one that buys the large speedup.

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

33 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