git log --oneline and --graph - Time & Space Complexity
We want to understand how the time to run git log --oneline --graph changes as the number of commits grows.
Specifically, how does showing a simple list with a graph affect performance?
Analyze the time complexity of this git command:
git log --oneline --graph
This command shows a short summary of each commit along with a text-based graph of branches and merges.
Look for repeated work done by the command:
- Primary operation: Reading and processing each commit in the history.
- How many times: Once for every commit in the repository's history.
The command processes each commit to build the graph and show the summary.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 commits | About 10 operations |
| 100 commits | About 100 operations |
| 1000 commits | About 1000 operations |
Pattern observation: The work grows directly with the number of commits.
Time Complexity: O(n)
This means the time to run the command grows linearly with the number of commits.
[X] Wrong: "The graph drawing makes the command take much longer than just listing commits."
[OK] Correct: The graph is built while reading commits once, so it does not add extra loops over commits. The main cost is still reading each commit once.
Understanding how commands scale with input size helps you explain performance in real projects. It shows you can think about efficiency beyond just writing code.
What if we added a filter to show only commits from a specific author? How would the time complexity change?