Mathematics I / Vectorised Computation and Libraries
Practice question · Sort into groups

Sort each code pattern by its style.

Groups: Vectorised · Explicit loop

Hints
  1. Look for whether an index appears anywhere in the code.
  2. Each pair here computes exactly the same thing in the two styles.
Show the answer

Vectorised: c = a + b, total = sum(a), d = 2 * a

Explicit loop: for each index i, set c[i] = a[i] + b[i], for each x in a, set total = total + x

Why

Vectorised code names whole arrays and no indices; the loop versions compute identical results far more slowly. The pairs are deliberately equivalent so that the only difference is the idiom, and hence the speed.

Read the lesson: Vectorised Computation and Libraries →

Practise Vectorised Computation and Libraries

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 Vectorised Computation and Libraries