Creating boolean arrays in NumPy - Performance & Efficiency
We want to understand how long it takes to create boolean arrays using numpy as the input size grows.
Specifically, how does the time needed change when we make bigger arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 1000000
arr = np.arange(n)
bool_arr = arr % 2 == 0
This code creates an array of numbers from 0 to n-1, then makes a boolean array marking even numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each element in the array to see if it is even.
- How many times: Once for every element, so n times.
As the array size grows, the time to create the boolean array grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to create the boolean array grows linearly with the number of elements.
[X] Wrong: "Creating a boolean array is instant and does not depend on array size."
[OK] Correct: Each element must be checked to decide True or False, so time grows with array size.
Understanding how array operations scale helps you explain efficiency clearly in interviews and shows you know how data size affects performance.
"What if we create a boolean array by comparing to a fixed value instead of using a modulo operation? How would the time complexity change?"