"This processor runs at 4 GHz" says how often its clock ticks, not how fast your program runs. The time a program takes on the CPU is:
CPU time = instruction count × CPI × clock period = instruction count × CPI ÷ clock rate
| Term | Meaning | Depends on |
|---|---|---|
| Instruction count (IC) | instructions the program runs | the program, the compiler, the ISA |
| CPI | average clock cycles per instruction | the processor's design, the instruction mix |
| Clock rate | ticks per second (1 GHz = 10⁹) | the circuit technology |
programs = {"A at 3 GHz, CPI 1.0": (2e9, 1.0, 3e9), "B at 4 GHz, CPI 2.0": (2e9, 2.0, 4e9)}
for name, (count, cpi, clock) in programs.items():
seconds = count * cpi / clock
print(name, "->", round(seconds, 3), "s")CPI from an instruction mix
Different instructions take different numbers of cycles. CPI is their weighted average:
mix = {"ALU": (0.50, 1), "load": (0.20, 5), "store": (0.10, 3), "branch": (0.20, 2)} # share, cycles
cpi = 0
for kind, (share, cycles) in mix.items():
cpi += share * cycles
print(kind, ":", share, "x", cycles, "cycles")
print("average CPI:", round(cpi, 2))Comparing two machines by clock rate alone misleads: a 3 GHz processor with CPI 1 beats a 4 GHz one with CPI 2 on the same program. MIPS (millions of instructions per second = clock rate ÷ CPI ÷ 10⁶) misleads too, because a machine with simpler instructions needs more of them. The only fair test is to run real programs: benchmarks such as SPEC CPU.
Amdahl's law
Speeding up one part helps only as much as that part matters. If a fraction p of the time can be made s times faster:
overall speedup = 1 ÷ ((1 − p) + p ÷ s)
Make 80% of a program 10 times faster and the whole is only 1 ÷ (0.2 + 0.08) ≈ 3.6 times faster. Even with an infinitely fast part, it can never beat 1 ÷ 0.2 = 5. The lesson: speed up the common case first.
Common mistakes
- Comparing processors by GHz only, ignoring CPI and instruction count.
- Forgetting units: 2 GHz means a clock period of 0.5 ns.
- Expecting a part sped up s times to speed up the whole program s times. Amdahl's law says otherwise.
Quick check
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.