Bird
0
0
Raspberry Piprogramming~5 mins

Capturing still images in Raspberry Pi - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Capturing still images
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for loop that calls camera.capture() repeatedly.
  • How many times: Exactly 5 times, once per image.
How Execution Grows With Input

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
1010 captures
100100 captures
10001000 captures

Pattern observation: Doubling the number of images doubles the total work and time.

Final Time Complexity

Time Complexity: O(n)

This means the time to capture images grows in a straight line with the number of pictures taken.

Common Mistake

[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.

Interview Connect

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.

Self-Check

"What if we changed the code to capture images in parallel instead of one after another? How would the time complexity change?"