sam
sam

Reputation: 15

Vectorizing Algorithms

In Numerical Methods in Engineering with Python 3, Author: Jaan Kiusalaas, page 25, the author solves a summation expression in two ways:

First method: using a loop.

Second method: a vectorized version by importing arange from numpy.

The author states that "The vectorized algorithm executes much faster, but uses more memory." Could anyone please explain what is meant by vectorizing algorithm and why does the vectorized algorithm executes much faster, but uses more memory?

Upvotes: 1

Views: 1861

Answers (1)

SergiyKolesnikov
SergiyKolesnikov

Reputation: 7795

Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times. [What is “vectorization”?]

  • Vectorized code is faster, because multiple operations are executed in parallel. In contrast, in the loop version, these operations are executed sequentially, one after another.

  • Vectorized code uses more memory, because the data for all operations, which are executed in parallel, have to be loaded into memory. In contrast, for the loop version, only the data for one operation (which is currently executed) have to be loaded.

It is a typical tradeoff in computer science known as Space–time tradeoff.

Upvotes: 4

Related Questions