Mathematics I / Reproducibility and Good Practice
Practice question · Multiple choice

A NumPy vectorised operation and an equivalent Python loop compute identical results, and the vectorised one can be fifty times faster. Where does the difference come from?

Hints
  1. Count how many times the Python interpreter is involved in each version.
  2. The arithmetic is identical. Ask what surrounds each operation.
Show the answer

B. From leaving the interpreter for compiled C over contiguous memory

Why

Same arithmetic, different overhead: a Python loop pays bytecode dispatch and type checks per element, while one NumPy call crosses into C once and runs a tight loop over contiguous memory with SIMD. It is why 'vectorise it' is the first optimisation in scientific Python, and why the advice does not transfer to C.

Read the lesson: Reproducibility and Good Practice →

Practise Reproducibility and Good Practice

The app has 5 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.

More questions on Reproducibility and Good Practice