---
title: Performance and the iron law
heading: Execution time from instruction count, CPI and clock rate, benchmarks, and the memory wall
eyebrow: S4 · Lesson 1 · Performance and the iron law
xp: 50
---

::: hook
Computer architecture is the art of spending a transistor and power budget to make programs run faster, and "faster" needs a precise meaning before any design choice can be judged. The only honest measure is the time a real program takes. That time factors into three terms (how many instructions run, how many cycles each takes on average, and how long a cycle is), and every architectural idea in this course attacks one of them, usually at some cost to another. This course builds on C8's processor and S3's hardware to the depth of a senior architecture course, using Hennessy and Patterson's quantitative approach: measure, model, then design.
:::

::: play
- Average time per instruction against clock frequency for a core whose pipeline CPI is $\text{CPI}_\text{core}$ and which misses to main memory m times per instruction, each miss taking a fixed $t_\text{mem}$ nanoseconds.
- The core's part, $\text{CPI}_\text{core}/f$, shrinks as the clock rises, but the memory part, $m\,t_\text{mem}$, doesn't, because DRAM's latency is fixed in nanoseconds.
- Raise m: past a point a faster clock barely helps. That's the memory wall, the reason a third of this course is about memory.
:::
:::

```sim plot
view: graph
x: f = 0.5..6 GHz
y: t = 0..3 ns
slider: c = 0.5..2 = 1
slider: m = 0..0.03 = 0.01
slider: M = 50..150 = 100 ns
plot: c/f + m*M | label: time per instruction, ns
plot: c/f | dashed | color: ch2 | label: if memory were free, ns
plot: m*M | dashed | color: ch3 | label: memory stalls, ns
readout: speedup from 1 to 4 GHz = (c + m*M)/(c/4 + m*M)
```

::: predict
## A core with a CPI of 1 also misses to a 100 ns memory once every 100 instructions. Its clock is raised from 1 GHz to 4 GHz. How much faster do programs run?

- [ ] 4 times
- [ ] 2.5 times
- [x] 1.6 times
- [ ] Not at all

::: reveal
1.6 times. At 1 GHz each instruction averages 1 ns in the core plus 0.01 × 100 = 1 ns of memory stall: 2 ns. At 4 GHz the core part shrinks to 0.25 ns but the stall stays at 1 ns: 1.25 ns. The memory stall became 80% of the time. Architects attack it with caches, prefetching and out-of-order execution that overlaps misses with useful work.
:::
:::

::: nameit
## The iron law

$$\text{CPU time} = \text{IC} \times \text{CPI} \times T_\text{clk} = \frac{\text{IC} \times \text{CPI}}{f}$$

- **Instruction count (IC):** set by the program, the compiler and the ISA.
- **Cycles per instruction (CPI):** set by the microarchitecture and the memory system; a mix's CPI is $\sum_i f_i\text{CPI}_i$. Its reciprocal is IPC.
- **Clock period:** set by the circuit design and the pipeline depth.

Improving one term often worsens another: a more powerful instruction lowers IC but may raise CPI or the period.

## Memory stalls

$$\text{CPI} = \text{CPI}_\text{core} + \frac{\text{misses}}{\text{instruction}} \times \text{miss penalty in cycles}$$

With the miss penalty fixed in nanoseconds, its cost in cycles grows with the clock rate.

## Comparing machines

- **Speedup** of B over A: $\frac{\text{time}_A}{\text{time}_B}$.
- **MIPS** ($f/(\text{CPI} \times 10^6)$) ignores instruction count, so it can rank machines backwards.
- **Benchmarks** such as SPEC report each program's ratio to a reference machine, combined by the **geometric mean**, $\left(\prod_i r_i\right)^{1/n}$, the only mean whose ranking doesn't depend on which machine is the reference.
- **Performance per watt** matters as much as performance from phones to data centers (lesson 3).
:::

::: example
## A program runs $10^9$ instructions at a CPI of 1.5 on a 2 GHz core. A new compiler cuts the instruction count by 20% but raises the CPI to 1.8. Which is faster, and which has the higher MIPS?

::: solution
1. **Time with the old compiler.**
   $$\begin{aligned} t_\text{old} &= \frac{10^9 \times 1.5}{2 \times 10^9}\ \text{s} \\ &= 0.75\ \text{s} \end{aligned}$$
2. **Time with the new compiler.**
   $$\begin{aligned} t_\text{new} &= \frac{0.8 \times 10^9 \times 1.8}{2 \times 10^9}\ \text{s} \\ &= 0.72\ \text{s} \end{aligned}$$
   so the new code is 4% faster.
3. **MIPS of each.**
   $$\text{old: } 2000/1.5 = 1333$$
   $$\text{new: } 2000/1.8 = 1111$$
4. **Why MIPS gets it wrong.** MIPS ranks them the wrong way, because it ignores the 20% fewer instructions.

**Answer:** the new compiler's code is 4% faster (0.72 s against 0.75 s), yet the old code has the higher MIPS (1333 against 1111).
:::
:::

::: example
## A new machine's speed ratios on three benchmarks are 2, 1.5 and 4. What single number does SPEC report, and why not the arithmetic mean?

::: solution
1. **The geometric mean.** SPEC reports the geometric mean of the ratios:
   $$\begin{aligned} G &= (2 \times 1.5 \times 4)^{1/3} \\ &= 2.29 \end{aligned}$$
2. **The arithmetic mean.** It is 2.5, but it gives a different ranking depending on which machine the ratios are taken against.
3. **Why the geometric mean is consistent.** In a geometric mean of ratios the reference machine's times cancel, so the ranking doesn't depend on the reference.
4. **Its weakness.** The geometric mean no longer corresponds to the time of any real workload.

**Answer:** SPEC reports the geometric mean, 2.29, because unlike the arithmetic mean (2.5) it ranks machines the same whichever machine the ratios are taken against.
:::
:::

::: example
## A core with a base CPI of 1.0 runs at 2 GHz; 2% of its instructions miss to a memory 100 ns away. What is its CPI, and how much faster is it at 4 GHz with the same memory?

::: solution
1. **CPI at 2 GHz.** A 100 ns miss costs 200 cycles:
   $$\begin{aligned} \text{CPI} &= 1 + 0.02 \times 200 \\ &= 5.0 \end{aligned}$$
   which is 2.5 ns per instruction.
2. **CPI at 4 GHz.** The same 100 ns is now 400 cycles:
   $$\begin{aligned} \text{CPI} &= 1 + 0.02 \times 400 \\ &= 9.0 \end{aligned}$$
   which is 2.25 ns per instruction.
3. **Compare the times.**
   $$2.5\ \text{ns} / 2.25\ \text{ns} = 1.11$$
   Doubling the clock buys only 11%: that's the memory wall in one line.

**Answer:** the CPI is 5.0 at 2 GHz; at 4 GHz it rises to 9.0, and the core is only 11% faster.
:::
:::

::: check
given: N = 0.5 | 2 | 8; C = 0.8 | 1.3 | 2.5; f = 1.5 | 3
answer: N*C/f
unit: s
tolerance: 0.5%

## A program runs {N} billion instructions at a CPI of {C} on a {f} GHz core. How long does it take?

::: right
Right. $\text{IC} \times \text{CPI}/f$ = {answer} s.
:::

::: misses
- [N/f] Include the CPI.
- [N*f/C] Time grows with CPI and shrinks with clock rate.
- [any] Not yet. Instructions × cycles each ÷ cycles per second.
:::
:::

::: check
given: a = 0.5 | 0.6; b = 0.25 | 0.3; x = 1 | 1.2; y = 2 | 3; z = 3 | 5
answer: a*x + b*y + (1 - a - b)*z
tolerance: 0.5%

## A program is {a} ALU instructions at CPI {x}, {b} loads and stores at CPI {y}, and the rest branches at CPI {z}. What's its CPI?

::: right
Right. $\sum f_i\text{CPI}_i$ = {answer}.
:::

::: misses
- [(x + y + z)/3] Weight each class by its frequency.
- [any] Not yet. Weighted average.
:::
:::

::: check
given: I = 1 | 1.5; C = 1.2 | 2; f = 2 | 3; J = 0.7 | 1.3; D = 1.5 | 2.5; g = 2.5 | 4
answer: (I*C/f)/(J*D/g)
tolerance: 0.5%

## Machine A runs a program as {I} billion instructions at CPI {C} and {f} GHz; machine B needs {J} billion instructions at CPI {D} and {g} GHz. What's B's speedup over A?

::: right
Right. $\text{time}_A/\text{time}_B$ = {answer}.
:::

::: misses
- [(J*D/g)/(I*C/f)] That's A's speedup over B.
- [g/f] Instruction count and CPI change too.
- [any] Not yet. Compute both times.
:::
:::

::: check
given: c = 0.8 | 1.2; m = 0.005 | 0.02; M = 60 | 120; f = 1 | 2; k = 2 | 4
answer: (c/f + m*M)/(c/(k*f) + m*M)
tolerance: 0.5%

## A core with CPI {c} misses {m} times per instruction to a {M} ns memory. Its clock goes from {f} GHz to {k}× that. What's the speedup?

::: right
Right. $\frac{c/f + mt_\text{mem}}{c/kf + mt_\text{mem}}$ = {answer}.
:::

::: misses
- [k] The memory stall time doesn't shrink with the clock.
- [any] Not yet. Time per instruction, before and after.
:::
:::

::: check
given: f = 2 | 3.5; C = 1.25 | 2
answer: f*1e9/(C*1e6)
tolerance: 0.5%

## What's the MIPS rating of a {f} GHz core with a CPI of {C}?

::: right
Right. $f/(\text{CPI} \times 10^6)$ = {answer} MIPS.
:::

::: misses
- [f*1e9*C/1e6] Divide by the CPI.
- [any] Not yet. Instructions per second, in millions.
:::
:::

::: check
given: a = 1.5 | 3; b = 0.8 | 2; c = 2.5 | 6
answer: (a*b*c)^(1/3)
tolerance: 0.5%

## A machine's speed ratios on three benchmarks are {a}, {b} and {c}. What's the geometric mean?

::: right
Right. $(abc)^{1/3}$ = {answer}.
:::

::: misses
- [(a + b + c)/3] SPEC uses the geometric mean of ratios.
- [any] Not yet. The cube root of the product.
:::
:::

::: check
kind: parts
given: I = 1 | 5; c = 0.8 | 1.5 | 2.5; f = 1 | 3

## A program executes {I} × 10⁹ instructions at a CPI of {c} on a {f} GHz core.

::: part
answer: I*c/f
unit: s
tolerance: 0.5%

### How long does it run?

::: misses
- [I*f/c] Time is instructions × CPI ÷ frequency.
- [any] $\text{IC} \times \text{CPI}/f$.
:::
:::

::: part
answer: f*1000/c
tolerance: 0.5%

### What MIPS rating does it get?

::: misses
- [any] $f/(\text{CPI} \times 10^6)$.
:::
:::
:::

::: check
kind: parts
given: c = 0.5 | 1; m = 0.005 | 0.02; p = 60 | 100; f = 2 | 4

## A core with a base CPI of {c} misses to memory on {m} of its instructions; memory takes {p} ns, and the clock is {f} GHz.

::: part
answer: c + m*p*f
tolerance: 0.5%

### What is its CPI?

::: misses
- [c + m*p] Convert the penalty to cycles: ns × GHz.
- [any] $\text{CPI}_\text{core} + m \times t_\text{mem}f$.
:::
:::

::: part
answer: (c + m*p*f)/f
unit: ns
tolerance: 0.5%

### How long does each instruction take on average?

::: misses
- [any] CPI ÷ f.
:::
:::
:::

::: check
kind: derivation
vars: a = 1..5, b = 1..5, c = 1..5

## A machine's speed ratios against a reference on three benchmarks are a, b and c.

::: step
answer: (a*b*c)^(1/3)

### What is their geometric mean?
:::

::: step
answer: (a + b + c)/3

### What is their arithmetic mean?
:::

::: step
answer: 3/(1/a + 1/b + 1/c)

### And their harmonic mean?
:::

::: right
Right. Only the geometric mean ranks machines the same whichever one is the reference, which is why SPEC uses it.
:::
:::

::: explain
## Why is execution time on real programs the only trustworthy performance measure, when clock rate, MIPS and peak FLOPS are all easier to quote?

::: model
Each of the easy numbers measures one factor of execution time and assumes the others stay fixed. Clock rate ignores how many cycles each instruction takes and how many instructions the program needs; MIPS ignores how many instructions are needed, so a machine with simpler instructions can show higher MIPS while finishing later; peak FLOPS assumes every unit is busy every cycle, which memory stalls and dependences prevent. Two designs can trade these factors against each other, so only the product, the time the program actually takes, tells which is faster. That's why architects measure real benchmarks and use the iron law to explain each result in terms of its three factors.
:::
:::

::: practice

::: check
given: N = 1 | 4; C = 0.9 | 1.7; f = 2.5 | 4
answer: N*C/f
unit: s
tolerance: 0.5%

## Time for {N} billion instructions, CPI {C}, {f} GHz?

::: misses
- [any] $\text{IC} \times \text{CPI}/f$.
:::
:::

::: check
given: c = 0.7 | 1.5; m = 0.004 | 0.015; M = 80 | 100; f = 1.5 | 2.5; k = 2 | 3
answer: (c/f + m*M)/(c/(k*f) + m*M)
tolerance: 0.5%

## Speedup from {k}× the clock: CPI {c}, {m} misses per instruction, {M} ns memory, {f} GHz?

::: misses
- [any] Compare time per instruction.
:::
:::

::: check
given: a = 1.2 | 2.4; b = 0.9 | 1.8; c = 3 | 4.5
answer: (a*b*c)^(1/3)
tolerance: 0.5%

## Geometric mean of {a}, {b} and {c}?

::: misses
- [any] Cube root of the product.
:::
:::
:::
