Challenge - 5 Problems
NumPy vs Python List Performance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Comparing sum performance of NumPy array and Python list
What is the output of the following code snippet that compares the sum of elements in a NumPy array and a Python list?
NumPy
import numpy as np import time arr = np.arange(1000000) lst = list(range(1000000)) start_np = time.time() sum_np = np.sum(arr) end_np = time.time() start_lst = time.time() sum_lst = sum(lst) end_lst = time.time() print(f"NumPy sum: {sum_np}, Time: {end_np - start_np:.6f} seconds") print(f"List sum: {sum_lst}, Time: {end_lst - start_lst:.6f} seconds")
Attempts:
2 left
💡 Hint
Think about how NumPy uses optimized C code for operations compared to Python's built-in sum.
✗ Incorrect
NumPy arrays use optimized C implementations for operations like sum, making them faster than Python lists which sum elements in pure Python loops.
❓ data_output
intermediate2:00remaining
Memory usage difference between NumPy array and Python list
What is the approximate memory usage difference when storing one million integers in a NumPy array vs a Python list?
NumPy
import numpy as np import sys arr = np.arange(1000000, dtype=np.int32) lst = list(range(1000000)) mem_arr = arr.nbytes mem_lst = sys.getsizeof(lst) + sum(sys.getsizeof(i) for i in lst) print(f"NumPy array memory: {mem_arr} bytes") print(f"Python list memory: {mem_lst} bytes")
Attempts:
2 left
💡 Hint
Consider that Python lists store references to objects, while NumPy arrays store raw data in contiguous memory.
✗ Incorrect
NumPy arrays store data in a compact, contiguous block of memory, using about 4 bytes per integer. Python lists store references to Python integer objects, which consume more memory, resulting in about 28 MB for one million integers.
❓ visualization
advanced2:00remaining
Visualizing performance difference of element-wise multiplication
Which option produces the correct matplotlib bar chart comparing the time taken for element-wise multiplication of a NumPy array vs a Python list?
NumPy
import numpy as np import time import matplotlib.pyplot as plt arr = np.arange(1000000) lst = list(range(1000000)) start_np = time.time() res_np = arr * 2 end_np = time.time() start_lst = time.time() res_lst = [x * 2 for x in lst] end_lst = time.time() times = [end_np - start_np, end_lst - start_lst] labels = ['NumPy array', 'Python list'] plt.bar(labels, times, color=['blue', 'orange']) plt.ylabel('Time (seconds)') plt.title('Element-wise multiplication performance') plt.show()
Attempts:
2 left
💡 Hint
NumPy uses vectorized operations that are faster than list comprehensions.
✗ Incorrect
NumPy's element-wise multiplication is implemented in optimized C code and runs much faster than Python list comprehensions, so the bar for NumPy time is much shorter.
🧠 Conceptual
advanced2:00remaining
Why are NumPy arrays faster than Python lists for numerical operations?
Which option best explains why NumPy arrays perform numerical operations faster than Python lists?
Attempts:
2 left
💡 Hint
Think about how data is stored and processed at a low level.
✗ Incorrect
NumPy arrays store data in contiguous memory and use compiled C code for operations, making them faster. Python lists store references to objects and operate in pure Python, which is slower.
🔧 Debug
expert2:00remaining
Identifying error in mixing NumPy arrays and Python lists in arithmetic
What error will this code raise?
import numpy as np
arr = np.array([1, 2, 3])
lst = [4, 5, 6]
result = arr + lst
print(result)
Attempts:
2 left
💡 Hint
NumPy supports adding arrays and lists element-wise by converting lists to arrays.
✗ Incorrect
NumPy automatically converts the Python list to an array and performs element-wise addition, resulting in a NumPy array [5 7 9]. No error is raised.