Template-based page generation in SEO Fundamentals - Time & Space Complexity
When generating web pages using templates, it's important to know how the time to create each page changes as the number of pages grows.
We want to understand how the work increases when more pages are made from templates.
Analyze the time complexity of the following template-based page generation code.
for page in pages:
load template
fill template with page data
save generated page
This code creates each page by loading a template, filling it with specific data, and saving the result.
Look for repeated steps that take most time.
- Primary operation: Looping over each page to generate content.
- How many times: Once for every page in the list.
As the number of pages increases, the total work grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 template fills |
| 100 | 100 template fills |
| 1000 | 1000 template fills |
Pattern observation: Doubling the pages doubles the work needed.
Time Complexity: O(n)
This means the time to generate pages grows directly with the number of pages.
[X] Wrong: "Loading the template once means generating many pages is almost free."
[OK] Correct: Each page still needs to be filled and saved, so work grows with pages, not stays constant.
Understanding how work grows with input size helps you explain efficiency clearly and shows you can think about real-world coding tasks.
"What if the template was loaded once outside the loop and reused? How would the time complexity change?"