Bird
Raised Fist0
NumPydata~20 mins

Profiling NumPy operations - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1: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)
AIt returns a NumPy array with the dot product result.
BIt prints the average execution time of the dot product operation multiple times.
CIt raises a SyntaxError because %timeit is invalid syntax in Python scripts.
DIt returns the shape of the resulting array from the dot product.
Attempts:
2 left
💡 Hint
Remember that %timeit is a magic command in Jupyter to measure execution time.
❓ data_output
intermediate
1: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))
AA small float number representing seconds, typically less than 0.01.
BThe sum of numbers from 0 to 999999, which is 499999500000.
CAn integer representing the number of elements in the array, 1000000.
DA string showing the start and end times concatenated.
Attempts:
2 left
💡 Hint
The variable duration measures elapsed time, not the sum value.
🔧 Debug
advanced
1: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)
ATypeError because np.linalg.inv expects a 1D array.
BNo error; it runs and prints timing information.
CSyntaxError: invalid syntax because %timeit is a Jupyter magic command.
DValueError because the matrix is singular and cannot be inverted.
Attempts:
2 left
💡 Hint
Consider where %timeit commands are valid.
🚀 Application
advanced
2: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:
NumPy
import numpy as np
import time
arr = np.random.rand(1000, 1000)

# Which timing method to use here?
AUse time.perf_counter() before and after the operation to measure elapsed time.
BUse %timeit magic command around the operation.
CUse print(time.time()) before and after, then subtract.
DUse np.profile() function to get timing.
Attempts:
2 left
💡 Hint
Magic commands like %timeit only work in Jupyter, and np.profile() does not exist.
🧠 Conceptual
expert
2: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?
ABecause the arrays are stored inefficiently causing slow access.
BBecause NumPy uses a recursive algorithm that doubles time each step.
CBecause the time depends only on the number of rows, not columns.
DBecause matrix multiplication involves n^3 scalar multiplications and additions.
Attempts:
2 left
💡 Hint
Think about how many operations are needed to multiply two n x n matrices.

Practice

(1/5)
1. What is the main purpose of profiling NumPy operations?
easy
A. To make the code look cleaner
B. To find which parts of the code run slowly
C. To check if the code has syntax errors
D. To add comments to the code

Solution

  1. Step 1: Understand profiling meaning

    Profiling means measuring how long parts of code take to run.
  2. Step 2: Identify profiling goal in NumPy

    In NumPy, profiling helps find slow operations to improve speed.
  3. Final Answer:

    To find which parts of the code run slowly -> Option B
  4. Quick Check:

    Profiling = Find slow parts [OK]
Hint: Profiling = measuring speed to find slow code [OK]
Common Mistakes:
  • Confusing profiling with debugging syntax errors
  • Thinking profiling cleans code
  • Assuming profiling adds comments
2. Which Python function is commonly used to measure time for profiling NumPy operations?
easy
A. time.time()
B. print()
C. len()
D. np.array()

Solution

  1. Step 1: Recall timing functions in Python

    Python's time.time() returns current time in seconds.
  2. Step 2: Use time.time() for profiling

    Subtracting start and end time.time() gives elapsed time.
  3. Final Answer:

    time.time() -> Option A
  4. Quick Check:

    Timing = time.time() [OK]
Hint: Use time.time() to get current time in seconds [OK]
Common Mistakes:
  • Using print() to measure time
  • Confusing len() with timing
  • Using np.array() instead of timing
3. What will be the output of this code snippet?
import numpy as np
import time
arr = np.arange(1000000)
start = time.time()
sum_val = np.sum(arr)
end = time.time()
print(round(end - start, 4))
medium
A. A very small number close to 0, like 0.001
B. The sum of numbers from 0 to 999999
C. An error because np.sum is wrong
D. The time in seconds as a large number like 1000

Solution

  1. Step 1: Understand code timing

    The code measures time to sum 1 million numbers using np.sum.
  2. Step 2: Expect timing output

    np.sum is fast; timing will be a small float near zero seconds.
  3. Final Answer:

    A very small number close to 0, like 0.001 -> Option A
  4. Quick Check:

    Timing output = small float [OK]
Hint: np.sum is fast; timing shows small seconds [OK]
Common Mistakes:
  • Thinking output is the sum value
  • Expecting an error from np.sum
  • Assuming timing is very large
4. Identify the error in this profiling code:
import numpy as np
import time
arr = np.arange(1000)
start = time.time()
sum_val = np.sum(arr)
print(round(end - start, 5))
end = time.time()
medium
A. round() cannot take two arguments
B. np.sum does not work on arrays
C. time.time() cannot be used twice
D. Variable 'end' is used before assignment

Solution

  1. Step 1: Check variable usage order

    The code prints end - start before end is assigned.
  2. Step 2: Identify error cause

    This causes a NameError because end is undefined at print time.
  3. Final Answer:

    Variable 'end' is used before assignment -> Option D
  4. Quick Check:

    Use end after assignment [OK]
Hint: Assign end before using it in print [OK]
Common Mistakes:
  • Thinking np.sum is invalid
  • Believing time.time() can't be called twice
  • Confusing round() arguments
5. You want to compare the speed of two NumPy operations on large arrays. Which approach is best for profiling?
hard
A. Run each operation once and compare times
B. Only check the code visually without timing
C. Run each operation many times and average the times
D. Use print statements inside the operations

Solution

  1. Step 1: Understand timing variability

    Running once can give misleading times due to random delays.
  2. Step 2: Use repeated runs for accuracy

    Running many times and averaging smooths out random noise.
  3. Final Answer:

    Run each operation many times and average the times -> Option C
  4. Quick Check:

    Average multiple runs for reliable timing [OK]
Hint: Average many runs to get stable timing [OK]
Common Mistakes:
  • Trusting single run timing
  • Ignoring timing and just reading code
  • Using print instead of timing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard