NumPy Lesson 22 – Performance Optimization | Dataplexa

Performance Optimization in NumPy

One of the biggest advantages of NumPy is its speed. NumPy is designed to perform numerical operations much faster than standard Python code.

In this lesson, you will learn how to write efficient NumPy code and avoid common performance mistakes.


Why NumPy Is Fast

NumPy is faster than pure Python because:

  • Operations are implemented in optimized C code
  • Memory is stored in contiguous blocks
  • Vectorized operations avoid Python loops

Understanding these concepts helps you write high-performance code.


Python Loops vs NumPy Vectorization

Let us compare a Python loop with a NumPy vectorized operation.

Using a Python Loop

import numpy as np

numbers = np.arange(1_000_000)
result = []

for x in numbers:
    result.append(x * 2)

This approach is slow because Python loops execute one step at a time.


Using NumPy Vectorization

numbers = np.arange(1_000_000)
result = numbers * 2

This performs the same operation much faster by leveraging optimized C code.


Avoid Unnecessary Copies

Creating unnecessary copies of arrays increases memory usage and slows down execution.

arr = np.arange(10)
view = arr[2:7]

view[0] = 99
print(arr)

Output:

[ 0  1 99  3  4  5  6  7  8  9]

This is faster because no extra memory is allocated.


Use Built-in NumPy Functions

Built-in NumPy functions are optimized and should be preferred over manual implementations.

Slow Approach

total = 0
for x in arr:
    total += x

Fast Approach

total = np.sum(arr)

NumPy functions execute much faster and use less memory.


Choose the Right Data Type

Using smaller data types improves speed and reduces memory usage.

small_ints = np.array([1, 2, 3, 4], dtype=np.int32)
large_ints = np.array([1, 2, 3, 4], dtype=np.int64)

Choose the smallest data type that fits your data.


Use Broadcasting Instead of Loops

Broadcasting allows operations on arrays of different shapes without explicit loops.

matrix = np.array([[1, 2, 3],
                   [4, 5, 6]])

result = matrix + 10
print(result)

Output:

[[11 12 13]
 [14 15 16]]

Use NumPy’s Logical Operations

Logical operations are faster than conditional loops.

data = np.array([10, 25, 30, 5, 18])
filtered = data[data > 20]
print(filtered)

Output:

[25 30]

Performance Tips Summary

  • Avoid Python loops
  • Use vectorized operations
  • Minimize copies
  • Use built-in NumPy functions
  • Choose efficient data types

Practice Exercise

Task

  • Create a large NumPy array
  • Multiply all values by 5 using vectorization
  • Filter values greater than a threshold
  • Calculate the sum efficiently

What’s Next?

In the next lesson, you will explore advanced broadcasting techniques to handle complex numerical operations efficiently.