Capturing still images in Raspberry Pi - Time & Space Complexity
When capturing still images on a Raspberry Pi, it is important to understand how the time taken grows as the number of images increases.
We want to know how the program's work changes when we take more pictures.
Analyze the time complexity of the following code snippet.
from picamera import PiCamera
from time import sleep
camera = PiCamera()
for i in range(5):
camera.capture(f'image_{i}.jpg')
sleep(1)
This code takes 5 pictures one after another, saving each with a unique name and pausing for 1 second between captures.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that callscamera.capture()repeatedly. - How many times: Exactly 5 times, once per image.
Each picture requires a fixed amount of time to capture and save. As the number of pictures increases, the total time grows directly with that number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 captures |
| 100 | 100 captures |
| 1000 | 1000 captures |
Pattern observation: Doubling the number of images doubles the total work and time.
Time Complexity: O(n)
This means the time to capture images grows in a straight line with the number of pictures taken.
[X] Wrong: "Taking more pictures doesn't increase time much because the camera works fast."
[OK] Correct: Each picture still takes a fixed time to capture and save, so more pictures add up and increase total time linearly.
Understanding how time grows with repeated actions like capturing images helps you explain and predict program performance clearly, a useful skill in many real projects.
"What if we changed the code to capture images in parallel instead of one after another? How would the time complexity change?"
