Script blocks for Groovy in Jenkins - Time & Space Complexity
We want to understand how the time it takes to run Groovy script blocks in Jenkins changes as the amount of work inside the block grows.
Specifically, how does adding more repeated tasks inside a script block affect the total execution time?
Analyze the time complexity of the following Jenkins Groovy script block.
node {
stage('Example') {
script {
for (int i = 0; i < n; i++) {
echo "Processing item ${i}"
}
}
}
}
This code runs a loop inside a Groovy script block that prints a message for each item from 0 to n-1.
Look for repeated actions inside the script block.
- Primary operation: The
forloop that runsechocommands. - How many times: Exactly
ntimes, wherenis the loop limit.
As n grows, the number of echo commands grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 echo commands |
| 100 | 100 echo commands |
| 1000 | 1000 echo commands |
Pattern observation: The total work grows directly in proportion to n. Double n, double the work.
Time Complexity: O(n)
This means the time to run the script block grows linearly with the number of loop iterations.
[X] Wrong: "The script block runs in constant time no matter how many times the loop runs."
[OK] Correct: Each loop iteration runs a command, so more iterations mean more work and more time.
Understanding how loops inside script blocks affect execution time helps you explain and predict Jenkins pipeline performance clearly and confidently.
"What if we nested another loop inside the script block? How would the time complexity change?"