0
0
NumPydata~20 mins

When NumPy is not fast enough - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy Speed Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Comparing execution time of NumPy and pure Python loops

What will be the output of the following code snippet that compares the sum of squares using NumPy and a Python loop?

NumPy
import numpy as np
import time

arr = np.arange(1_000_000)

start = time.time()
result_numpy = np.sum(arr ** 2)
end = time.time()
numpy_time = end - start

start = time.time()
result_loop = 0
for x in arr:
    result_loop += x ** 2
end = time.time()
loop_time = end - start

print(f"NumPy sum: {result_numpy}, Time: {numpy_time:.4f} seconds")
print(f"Loop sum: {result_loop}, Time: {loop_time:.4f} seconds")
A
NumPy sum: 333332833333500000, Time: 1.2000 seconds
Loop sum: 333332833333500000, Time: 0.0050 seconds
B
NumPy sum: 333332833333500000, Time: 0.0050 seconds
Loop sum: 333332833333500000, Time: 1.2000 seconds
C
NumPy sum: 500000500000, Time: 0.0050 seconds
Loop sum: 500000500000, Time: 1.2000 seconds
D
NumPy sum: 333332833333500000, Time: 0.0050 seconds
Loop sum: 0, Time: 1.2000 seconds
Attempts:
2 left
💡 Hint

NumPy uses optimized C code internally, so it runs much faster than Python loops.

data_output
intermediate
1:30remaining
Result shape after applying a slow Python function with np.vectorize

Given the code below, what is the shape of the output array?

NumPy
import numpy as np

def slow_func(x):
    return x ** 2 + 1

vec_func = np.vectorize(slow_func)
arr = np.arange(12).reshape(3,4)
result = vec_func(arr)
print(result.shape)
A(3, 4)
B(12,)
C(4, 3)
D(1, 12)
Attempts:
2 left
💡 Hint

np.vectorize preserves the input array shape.

🔧 Debug
advanced
2:30remaining
Why does this Numba JIT function run slower than NumPy?

Consider this code using Numba's JIT to speed up a function. Why might it run slower than the pure NumPy version?

NumPy
import numpy as np
from numba import jit

@jit(nopython=True)
def sum_squares(arr):
    total = 0
    for i in range(arr.size):
        total += arr[i] ** 2
    return total

arr = np.arange(1_000_000)

import time
start = time.time()
result_numba = sum_squares(arr)
end = time.time()
numba_time = end - start

start = time.time()
result_numpy = np.sum(arr ** 2)
end = time.time()
numpy_time = end - start

print(f"Numba time: {numba_time:.4f} seconds")
print(f"NumPy time: {numpy_time:.4f} seconds")
ANumba runs slower because np.arange is incompatible with Numba.
BNumba runs slower because the JIT compiler is not installed correctly.
CNumba runs slower because the function uses a Python loop instead of vectorized operations.
DNumba runs slower because the function is missing a return statement.
Attempts:
2 left
💡 Hint

Numba speeds up loops but cannot beat fully vectorized NumPy operations.

🧠 Conceptual
advanced
1:30remaining
When to consider alternatives to NumPy for speed

Which scenario is the best reason to consider alternatives like Numba or Cython instead of pure NumPy?

AWhen you want to use built-in NumPy functions for matrix multiplication.
BWhen you want to plot data using matplotlib.
CWhen you want to load data from CSV files quickly.
DWhen you need to speed up element-wise operations that cannot be vectorized easily.
Attempts:
2 left
💡 Hint

Think about when vectorization is not possible or practical.

🚀 Application
expert
2:30remaining
Optimizing a custom function with Numba for large arrays

You have a custom function that applies a complex formula element-wise on a large NumPy array. The pure Python loop is too slow. Which approach will most likely give the best speedup?

AUse Numba's @jit decorator with nopython=True on the loop function.
BRewrite the function using only Python built-in functions without NumPy.
CUse np.vectorize to apply the function element-wise.
DConvert the array to a list and use a Python list comprehension.
Attempts:
2 left
💡 Hint

Consider which method compiles loops to fast machine code.