Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

performance-vectorization

Performance and vectorization

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Performance and vectorization: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Performance and vectorization in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Performance and vectorization in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When basics, ufuncs, broadcasting, and simple linear algebra from intermediate lessons are comfortable in the playground.

NumPy performance comes from contiguous memory, ufuncs, and optional BLAS/LAPACK backends. Profile before micro-optimizing; vectorization beats Python loops by orders of magnitude.

Anti-patterns

  • Python for loops over elements
  • Repeated allocation inside loops
  • Object dtype arrays
  • Excessive fancy indexing in hot paths

Best practices

  • Express logic with ufuncs and axis reductions
  • Preallocate output with np.empty and out= parameter
  • Use broadcasting instead of tile/repeat when possible
  • Consider float32 if precision allows

Timing sketch

import numpy as np
a = np.arange(1_000_000)
print('vectorized sum:', a.sum())
print('prefer this over sum(a[i] for i in range(len(a)))')

Important interview questions and answers

  1. Q: Why loops slow?
    A: Python per-element overhead; NumPy batches in C.
  2. Q: BLAS?
    A: Basic Linear Algebra Subprograms—optimized matmul behind @ operator.

Self-check

  1. Name two vectorization best practices.
  2. Why avoid object dtype for numeric work?

Pitfall: Object dtype arrays disable fast vectorized paths.

Interview prep

Anti-pattern?

Python for-loop over millions of elements.

BLAS?

Optimized linear algebra behind @ operator.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Avoid Python loop?
  • Profiling habit?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump