Bird
0
0
Raspberry Piprogramming~5 mins

Time-lapse photography in Raspberry Pi - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Time-lapse photography
O(n)
Understanding Time Complexity

When creating a time-lapse, the program takes many photos over time. We want to understand how the time to complete the task grows as we take more photos.

How does the number of photos affect the total running time?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


import time
from picamera import PiCamera

camera = PiCamera()

for i in range(num_photos):
    camera.capture(f'image_{i}.jpg')
    time.sleep(interval_seconds)

This code takes a set number of photos, waiting a fixed time between each shot.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Taking a photo with camera.capture() inside the loop.
  • How many times: Exactly num_photos times, once per loop iteration.
How Execution Grows With Input

Each photo adds one capture operation and one wait period, so total time grows directly with the number of photos.

Input Size (n)Approx. Operations
1010 captures + 10 waits
100100 captures + 100 waits
10001000 captures + 1000 waits

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

Final Time Complexity

Time Complexity: O(n)

This means the total time grows in a straight line as you take more photos.

Common Mistake

[X] Wrong: "Taking more photos only adds a little extra time, so it's almost constant."

[OK] Correct: Each photo requires a full capture and wait, so time adds up directly with the number of photos.

Interview Connect

Understanding how loops affect time helps you explain how programs scale, a key skill in many coding tasks.

Self-Check

"What if we removed the wait time between photos? How would the time complexity change?"