Why AI generates text word by word in AI for Everyone - Performance Analysis
When AI generates text word by word, it processes each word step by step. We want to understand how the time it takes grows as the text gets longer.
How does the AI's work increase when it writes more words?
Analyze the time complexity of the following code snippet.
for i in range(n):
word = generate_next_word(context)
context.append(word)
output.append(word)
This code shows AI generating text one word at a time, updating context each step.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating the next word based on current context.
- How many times: Once for each word in the output, so n times.
Each new word requires the AI to look at all previous words to decide the next one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 (sum of 1 to 10) |
| 100 | About 5,050 |
| 1000 | About 500,500 |
Pattern observation: The work grows much faster than the number of words; it grows roughly like the square of n.
Time Complexity: O(n²)
This means if you double the number of words, the work more than doubles, growing roughly with the square of the length.
[X] Wrong: "Generating each word takes the same fixed time, so total time is just n times."
[OK] Correct: Each word depends on all previous words, so the time to generate grows as the text gets longer, not stays fixed.
Understanding how AI text generation time grows helps you explain performance in real projects. It shows you how tasks that build on previous results can become slower as they get bigger.
"What if the AI only looked at a fixed number of previous words instead of all of them? How would the time complexity change?"