Parallel stages execution in Jenkins - Time & Space Complexity
When Jenkins runs stages in parallel, it can do many tasks at the same time.
We want to see how the total time changes as we add more parallel stages.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Parallel Work') {
parallel {
stage('Task 1') {
steps { echo 'Running Task 1' }
}
stage('Task 2') {
steps { echo 'Running Task 2' }
}
}
}
}
}
This code runs two tasks at the same time inside a parallel block.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running each parallel stage's steps.
- How many times: Once per stage, but all run simultaneously.
Adding more parallel stages means more tasks start at the same time.
| Input Size (n) | Approx. Operations |
|---|---|
| 2 | 2 tasks run simultaneously |
| 10 | 10 tasks run simultaneously |
| 100 | 100 tasks run simultaneously |
Pattern observation: Total time depends on the longest single task, not the number of tasks.
Time Complexity: O(1)
This means the total time stays about the same no matter how many parallel stages run, assuming enough resources.
[X] Wrong: "More parallel stages always take more total time because they add up."
[OK] Correct: Parallel stages run at the same time, so total time depends on the slowest task, not the count.
Understanding parallel execution helps you explain how to speed up builds by running tasks together, a useful skill in real projects.
What if the parallel stages share limited resources and cannot truly run at the same time? How would the time complexity change?