Raspberry Pi Camera setup - Time & Space Complexity
When setting up a Raspberry Pi camera, some code runs repeatedly to capture or process images. Understanding how the time needed grows helps us know if the setup will work smoothly as tasks get bigger.
We want to see how the time to run the camera code changes when we capture more frames or process more data.
Analyze the time complexity of the following code snippet.
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(2) # Let camera adjust
for i in range(5):
camera.capture(f'image_{i}.jpg')
camera.stop_preview()
This code sets up the camera, shows a preview, then takes 5 pictures one after another before stopping the preview.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that captures images 5 times.
- How many times: Exactly 5 times, once per image.
As the number of pictures to capture increases, the total time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 captures |
| 100 | 100 captures |
| 1000 | 1000 captures |
Pattern observation: Doubling the number of pictures doubles the work and time needed.
Time Complexity: O(n)
This means the time to run the camera capture grows directly with the number of pictures taken.
[X] Wrong: "Taking more pictures won't affect the total time much because the camera is fast."
[OK] Correct: Each picture requires the camera to capture and save data, so more pictures mean more work and more time.
Understanding how loops affect time helps you explain how your code will behave as tasks grow, a key skill in real projects and interviews.
"What if we added image processing inside the loop after each capture? How would the time complexity change?"
