Challenge - 5 Problems
NumPy Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of timing a NumPy operation with %timeit
What is the output type when you run the following code in a Jupyter notebook cell?
import numpy as np arr = np.random.rand(1000, 1000) %timeit np.dot(arr, arr)
NumPy
import numpy as np arr = np.random.rand(1000, 1000) %timeit np.dot(arr, arr)
Attempts:
2 left
💡 Hint
Remember that %timeit is a magic command in Jupyter to measure execution time.
✗ Incorrect
The %timeit magic command runs the code multiple times and prints the average time taken. It does not return the result of the operation or any array.
❓ data_output
intermediate1:30remaining
Result of profiling a NumPy sum operation with time.perf_counter
What is the value of the variable
duration after running this code?import numpy as np import time arr = np.arange(1000000) start = time.perf_counter() s = np.sum(arr) end = time.perf_counter() duration = end - start print(round(duration, 5))
NumPy
import numpy as np import time arr = np.arange(1000000) start = time.perf_counter() s = np.sum(arr) end = time.perf_counter() duration = end - start print(round(duration, 5))
Attempts:
2 left
💡 Hint
The variable duration measures elapsed time, not the sum value.
✗ Incorrect
duration is the difference between end and start times, so it is a small float showing how long np.sum took.
🔧 Debug
advanced1:30remaining
Identify the error in profiling a NumPy operation
What error will this code raise when run in a standard Python script (not Jupyter)?
import numpy as np arr = np.random.rand(1000, 1000) %timeit np.linalg.inv(arr)
NumPy
import numpy as np arr = np.random.rand(1000, 1000) %timeit np.linalg.inv(arr)
Attempts:
2 left
💡 Hint
Consider where %timeit commands are valid.
✗ Incorrect
%timeit is a magic command only available in Jupyter or IPython environments, not in standard Python scripts.
🚀 Application
advanced2:00remaining
Choosing the best method to profile a NumPy function in a script
You want to measure how long a NumPy matrix multiplication takes inside a Python script (not Jupyter). Which method is best to get accurate timing?
Options:
Options:
NumPy
import numpy as np import time arr = np.random.rand(1000, 1000) # Which timing method to use here?
Attempts:
2 left
💡 Hint
Magic commands like %timeit only work in Jupyter, and np.profile() does not exist.
✗ Incorrect
time.perf_counter() provides high-resolution timing suitable for scripts. time.time() is less precise. %timeit is invalid outside Jupyter. np.profile() is not a real function.
🧠 Conceptual
expert2:30remaining
Understanding the impact of array size on NumPy operation profiling
You profile np.dot on arrays of increasing size and notice the time grows roughly with the cube of the dimension (n). Why does the time complexity behave like this for matrix multiplication?
Attempts:
2 left
💡 Hint
Think about how many operations are needed to multiply two n x n matrices.
✗ Incorrect
Standard matrix multiplication requires n^3 operations: for each of n rows and n columns, n multiplications and additions happen.