Cooling fan control in 3D Printing - Time & Space Complexity
When controlling a cooling fan in 3D printing, it's important to understand how the control process scales as the print progresses.
We want to know how the time to decide fan speed changes as the number of print layers or commands increases.
Analyze the time complexity of the following cooling fan control code snippet.
for each layer in print_layers:
temperature = read_temperature_sensor()
if temperature > threshold:
set_fan_speed(high)
else:
set_fan_speed(low)
wait_for_next_layer()
This code checks the temperature once per layer and adjusts the fan speed accordingly during a 3D print.
- Primary operation: Looping through each print layer to read temperature and set fan speed.
- How many times: Once per layer, so the number of layers determines repetitions.
As the number of layers increases, the number of temperature checks and fan speed adjustments grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 layers | 10 temperature checks and fan adjustments |
| 100 layers | 100 temperature checks and fan adjustments |
| 1000 layers | 1000 temperature checks and fan adjustments |
Pattern observation: The operations increase directly with the number of layers, growing steadily.
Time Complexity: O(n)
This means the time to control the fan grows in direct proportion to the number of layers printed.
[X] Wrong: "The fan control runs instantly and does not depend on the number of layers."
[OK] Correct: Each layer requires a temperature check and fan adjustment, so more layers mean more operations.
Understanding how control loops scale helps you explain how embedded systems manage resources efficiently during printing.
"What if the fan speed was adjusted only every 5 layers instead of every layer? How would the time complexity change?"