Bird
0
0
Raspberry Piprogramming~5 mins

Displaying text on OLED in Raspberry Pi - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Displaying text on OLED
O(n)
Understanding Time Complexity

When we display text on an OLED screen using a Raspberry Pi, the time it takes depends on how much text we want to show.

We want to understand how the time to display grows as the text gets longer.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

from luma.core.interface.serial import i2c
from luma.oled.device import sh1106
from PIL import Image, ImageDraw, ImageFont

serial = i2c(port=1, address=0x3C)
device = sh1106(serial)

text = "Hello, Raspberry Pi OLED!"

image = Image.new('1', device.size)
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, fill=255)
device.display(image)

This code writes a line of text onto the OLED screen by drawing it on an image and then sending it to the display.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Drawing each character of the text onto the image buffer.
  • How many times: Once for each character in the text string.
How Execution Grows With Input

As the text length increases, the number of characters to draw grows, so the time to draw grows too.

Input Size (n)Approx. Operations
10Draw 10 characters
100Draw 100 characters
1000Draw 1000 characters

Pattern observation: The time grows directly with the number of characters; double the characters means about double the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to display text grows in a straight line with the length of the text.

Common Mistake

[X] Wrong: "Displaying text takes the same time no matter how long the text is."

[OK] Correct: Each character must be drawn one by one, so longer text takes more time.

Interview Connect

Understanding how display time grows with text length helps you write efficient code for devices with limited speed and memory.

Self-Check

"What if we changed the code to display multiple lines of text instead of one? How would the time complexity change?"