Python Interactive Mode vs Script Mode - Performance Comparison
We want to understand how running Python code in interactive mode compares to running it as a script in terms of time cost.
How does the way we run code affect how long it takes to execute as input size grows?
Analyze the time complexity of this simple Python code run in two modes.
for i in range(n):
print(i)
This code prints numbers from 0 up to n-1, either typed line-by-line in interactive mode or run all at once as a script.
Look at what repeats when the code runs.
- Primary operation: The loop runs print(i) for each number.
- How many times: Exactly n times, once per number.
As n grows, the number of print operations grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the number of items printed.
[X] Wrong: "Interactive mode runs each line instantly, so it's always faster than script mode."
[OK] Correct: Both modes run the same code operations; interactive mode just waits for input each time, but the total work for n lines is still proportional to n.
Understanding how code execution time grows helps you explain performance differences clearly, whether running code interactively or as a script.
"What if we added a nested loop inside the for loop? How would the time complexity change?"