A pipeline fills once and then finishes one instruction per cycle. Compare it with a machine that runs one instruction at a time.
What to build
A program that prints the cycle counts with and without pipelining, and the speedup.
Requirements
- Print
pipelined Cwith C = k + n − 1 andunpipelined Uwith U = k × n. - Print
speedup Swith S = U ÷ C to 2 decimal places.
Input
One line: k (stages) and n (instructions).
Output
Three lines.
Constraints
1 ≤ k ≤ 30; 1 ≤ n ≤ 10⁶.
Example
Input:
5 100
Output:
pipelined 104
unpipelined 500
speedup 4.81
Why it works
The first instruction needs k cycles to pass through all stages, and every later one finishes one cycle after the one before it, which adds n − 1 cycles.
Examples
5 100
pipelined 104 unpipelined 500 speedup 4.81
5 1
pipelined 5 unpipelined 5 speedup 1.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Pipelined cycles = k + n - 1; unpipelined cycles = k * n.
Hint 2
The speedup is unpipelined ÷ pipelined; print it with f"{x:.2f}".
Solution
Show one correct solution (try it yourself first)
k, n = map(int, input().split())
c, u = k + n - 1, k * n
print("pipelined", c)
print("unpipelined", u)
print(f"speedup {u / c:.2f}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step