The clock of a pipeline must fit its slowest stage, plus a little time for the registers between stages. Work out how much a pipeline really gains.
What to build
A program that compares a single-cycle processor with a pipelined one for a program of n instructions.
Requirements
- The stage delays are given in picoseconds, and every pipeline register adds r picoseconds to each stage.
- Print
single clock Swhere S is the sum of the delays, andpipelined clock Pwhere P is the largest delay plus r. - Print
single totalas n × S andpipelined totalas (k + n − 1) × P, where k is the number of stages. - Print
speedup Xwith X = single total ÷ pipelined total to 3 decimal places.
Input
A line with the stage delays, a line with r, and a line with n.
Output
Five lines.
Constraints
2 ≤ k ≤ 20 stages; delays and r are whole numbers from 1 to 2000; 1 ≤ n ≤ 10⁶.
Example
Input:
250 150 200 300 100
20
1000
Output:
single clock 1000
pipelined clock 320
single total 1000000
pipelined total 321280
speedup 3.113
Why it works
A pipeline starts a new instruction every P picoseconds but needs k + n − 1 such periods. Unbalanced stages and register overhead make P larger than S ÷ k, so the speedup is below k.
Examples
250 150 200 300 100 20 1000
single clock 1000 pipelined clock 320 single total 1000000 pipelined total 321280 speedup 3.113
100 100 100 0 1
single clock 300 pipelined clock 100 single total 300 pipelined total 300 speedup 1.000
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The single-cycle clock is the sum of the stage delays. The pipelined clock is the largest stage delay plus the register overhead.
Hint 2
Total time without a pipeline is n × single clock; with a pipeline it is (k + n − 1) × pipelined clock.
Solution
Show one correct solution (try it yourself first)
delays = list(map(int, input().split()))
r = int(input())
n = int(input())
k = len(delays)
s = sum(delays)
p = max(delays) + r
print("single clock", s)
print("pipelined clock", p)
print("single total", n * s)
print("pipelined total", (k + n - 1) * p)
print(f"speedup {n * s / ((k + n - 1) * p):.3f}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step